Recently, because of the project needs, the need to press a button to return to the specified activity, there may be a succession of several activities, the first thought is to write a few more finish () ... Then think about it is impossible, check the activity of the relevant information, if need to fall back to activity A, then set A's startup mode to Singletask, in manifest.xml a statement inside add a word, Android:: Launchmode = "Singletask", which of course causes all activity on the activity stack to be erased every time a is started.
This is really no problem, and when the program returns, it invokes the finish () method of all activity on the activity stack and then reuses a. Because by the way to see some of the ACTIVITY of the flags, so looked at, feel like Flag_activity_clear_top can also do this effect, considering everyone seems to be with flag_activity_new_task together, Although reading the document feels Flag_activity_new_task is superfluous, but also consider if the fallback activity does not exist in the case .... Make it a little bit, and then you have:
Intent Intent = new Intent (this,a.class);
Intent.addflags (intent.flag_activity_clear_top| Intent.flag_activity_new_task);
StartActivity (Intent);
And then the crash started. Looked for a long time to find:
Flag_activity_clear_top: If there is already an instance of an activity type that responds to this intent, all activity on top of the instance will be cleaned up so that it is on top of the stack to respond to intent. if the activity specified at this time is loaded with " standard " (that is, by default), it is itself removed from the stack and a new instance is loaded to handle the incoming intent. This is because the activity that loads the mode " standard " always creates a new instance to handle the new intent.
That is, in this case, a will be destroyed and re-created, so if you rewrite the OnDestroy method of a, release resources, it will lead to two release of resources, if we set the load mode for singletask, then do not need to addflags, the direct start can be ... By the way:
FLAG_ACTIVITY_NEW_TASK: If there is a task that has the same affinity as the new activity, the activity will be loaded into that task. If not, the new task is enabled.
In addition, the related blog post also reminds us that we can also create a list in the application subclass to manage activity, the main method is to inherit the interface, overriding the Onactivitycreated,onactivitydestroy method, Corresponding to the List.add, List.remove method, when we need to return to a specific activity, we can be in the list of its activity above the finish (), specifically not verified, because directly set up Android:: Launchmode = "Singletask" is relatively simple, hehe ~
About the flag and boot mode of Android activity boot