Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flag Activity Clear Top destroys target activity and than creating it

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?

like image 857
kaushal trivedi Avatar asked Jul 06 '13 19:07

kaushal trivedi


People also ask

What is Flag activity clear top?

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() ).

What is true about flag activity new task?

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 ...

What is Flag activity?

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.

Which method is used to destroy an activity in android?

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.


2 Answers

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);
like image 132
Ryan S Avatar answered Oct 19 '22 22:10

Ryan S


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().

like image 42
Darpan Avatar answered Oct 19 '22 20:10

Darpan