Reputation:
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);
Upvotes: 0
Views: 2258
Reputation: 10278
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.
Upvotes: 0
Reputation: 4821
Intent intent = new Intent(myActivity.this,
myActivity.class);
intent .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Upvotes: 0
Reputation: 4620
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);
Upvotes: 2