Reputation: 5168
I have an app that starts a sequence of dialog-themed activities and I want to be able to pop them all off at once and go back to the main activity. I looked over existing questions like:
How to clear current activities in the stack?
how to kill sub activities and bring activity to top of stack
Android Popping off the Activity Stack
And based on that came up with this:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This works functionally, but the problem I have is that when this is executed, the screen behind the dialog-themed activity turns black for a second before finally animating the dialogs off the screen. If I pop these activities manually with a back button this does not happen.
In LogCat I can see that when I pop the dialogs using the Intent
method above, the main activity is destroyed and re-created, whereas when I just use the back button, the onDestroy
and onCreate
methods do not run. Is there any way to prevent the main activity from being explicitly re-started this way?
Upvotes: 0
Views: 3383
Reputation: 4393
You could just add singleTop
to your main activity.
Here, read about it. It brings the existing instance of the activity rather than create a new one.
http://developer.android.com/guide/topics/manifest/activity-element.html
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP
Upvotes: 1
Reputation: 8747
What about if you, when you add your main activity to the backstack you add it with a tag other than null like:
transaction.addToBackStack("welcome");
And then you can just pop the backstack like such:
FragmentManager fm = getFragmentManager();
fm.popBackStack("welcome", FragmentManager.POP_BACK_STACK_INCLUSIVE);
Upvotes: 0