简体   繁体   中英

Android. How to start activity without creating new one?

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 myIntent = 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM