FLAG_ACTIVITY_CLEAR_TOP android: launchMode = "singleInstance"

I think I just discovered a really strange mistake ... But it could be some feature I have never heard of.

In my application, if I have activity on AndroidManifest with android: launchMode = "singleInstance", when you try to "clear" the stack to a specific point using the following code:

Intent intent = new Intent(this, Xpto.class); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

This applies to this activity. But when you push back, it goes back to the previous one. It should have been finished ...

Example:

A → B → C

Then from C, I call A with Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP with one instance in the manifest. It goes to A, but it only brings it forward. And does not finish C and B.

Can someone explain this behavior?

The Xpto class that I call is the root action of the stack at that time.

+4
source share
1 answer

from reading this thread:

http://groups.google.com/group/android-developers/browse_thread/thread/5eb400434e2c35f4

it seems that:

"The current executable instance of action B in the above example will either get the new intent that you start here in the onNewIntent () method, or it will be completed and restarted with the new intent. If it declared that its launch mode is" multiple "( by default), and you do not set FLAG_ACTIVITY_SINGLE_TOP in the same intention, then it will be finished and recreated; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP, then this intention will be delivered to the current instance of onNewIntent (). "

which means you need to set your startMode to multiple instances and use only FLAG_ACTIVITY_CLEAR_TOP.

 Intent intent = new Intent(this, Xpto.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

In the scenario you described, activity B and C is not complete when you start activity A (which is the root activity). The documentation describes that in the singleInstance run mode and the FLAG_ACTIVITY_SINGLE_TOP flag is set, actions B and C will NOT be completed. If you want to complete steps B and C, you must set the startup mode for multiple instances and DO NOT set the FLAG_ACTIVITY_SINGLE_TOP flag.

+3
source

All Articles