I am watching a behavior of Intent.FLAG_ACTIVITY_CLEAR_TOP.
For example i have three activities A,B and C Now Flow is A -> B -> C
Now when i am starting A from C with this flag with following code.
Intent intent_to_a=new Intent(C.this,A.class);
intent_to_home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent_to_a);
AFAIK, Intent.FLAG_ACTIVITY_CLEAR_TOP should remove B and should resume the A .It also does the same but in a strange way. It removes B , than removes A than creates A. Method onDestroy of A is also being called. Can anyone tell me is it proper or not? If i don't want it to get destroy what should i do?
FLAG_ACTIVITY_CLEAR_TOP. If the activity being started is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent() ).
flag — FLAG_ACTIVITY_CLEAR_TOP: If the Activity being started is already running in the current task then instead of launching the new instance of that Activity, all the other activities on top of it is destroyed (with call to onDestroy method) and this intent is delivered to the resumed instance of the Activity (now ...
Flags exist to create a new activity, use an existing activity, or bring an existing instance of an activity to the front. For example, it's common to launch an activity when the user taps a notification. Often, apps will use the default intent flags, resulting in multiple copies of the same activity in the back stack.
The onStop() and onDestroy() methods get called, and Android destroys the activity. A new activity is created in its place. The activity is visible but not in the foreground.
Use FLAG_ACTIVITY_REORDER_TO_FRONT and then use an intent to tell B to finish.
Activity B:
private BroadcastReceiver finishReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
public void onCreate() {
LocalBroadcastManager.getInstance(this)
.registerReceiver(finishReceiver ,
new IntentFilter("B-finish"));
}
public void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(
finishReceiver );
}
Activity C:
LocalBroadcastManager.getInstance(this).sendBroadcast(
new Intent("B-finish"));
Intent intent_to_a=new Intent(C.this,A.class);
intent_to_home.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent_to_a);
Either,
1. Change the launchMode
of the Activity A to something else from standard (ie singleTask
or something). Then your flag FLAG_ACTIVITY_CLEAR_TOP
will not restart your Activity A.
or,
2. Use Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
as your flag. Then it will work the way you desire.
This question has good discussion on same topic
Android documentation says -
The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent.
If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With