开发者

Android. How to start activity without creating new one?

开发者 https://www.devze.com 2023-04-10 22:53 出处:网络
I tried to set attribute android:launchMode=\"singleTask\" for calling Activity, but it still does not works as I expected.

I tried to set attribute android:launchMode="singleTask" for calling Activity, but it still does not works as I expected.

I need that method onCreate(Bundle bundle) to be called only once, but it still calls each time when I start Activity.

I start Activity using code like this:

public void onClick(View v) {
    Intent myIn开发者_StackOverflowtent = new Intent(v.getContext(), NextActivity.class);
    startActivity(myIntent);
}

Please let me know what I am doing wrong


It must be like this:

android:launchMode="singleTop"

and calling:

Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Already existed topic about this: Android: new Intent() starts new instance with android:launchMode="singleTop"


Even if you make your launch mode 'singleTop', a new activity will be started.

At each activity creation, its onCreate() will be started.

My understanding is that the singleTop option, basically finishes the caller Activity.

I think that you may think that onCreate() is a form of Application constructor but it really is an Activity constructor. You may want to do your one time initializations elsewhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号