简体   繁体   中英

Within Android, how do I start an activity or bring to front that activity if already on stack?

Within Android, how do I start an activity or bring to front that activity if already on stack?

So far I know start a new activity myActivity , as by the code below shows. But I don't want to open a second copy of the same class, if it's already open on the stack. So how do I check whether activities like the below exist on the stack, then if it exists bring it to front. If it doesn't, open a new activity like below

Intent intent = new Intent(myActivity.this,
        myActivity.class);
startActivity(intent);

Use a flag in your intent when you want to start new Activity

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

Intent intent = new Intent(myActivity.this,
        myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Intent intent = new Intent(myActivity.this,
        myActivity.class);
 intent .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

It sounds like you might have several processes running or other instances of the Activity started through other calls to start the Activity.

You are probably needing to specify in the manifest either "singleInstance" or "singleTask" -

Android singleTask or singleInstance launch mode?

Word of caution - this may have some other unintended side effects. Just be sure to test it for different use-cases.

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