8

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?

kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47
  • 2
    look into FLAG_ACTIVITY_REORDER_TO_FRONT, this is closer to what you want http://developer.android.com/reference/android/content/Intent.html – Ryan S Jul 06 '13 at 19:46
  • 1
    yeah,i looked into that too,but i also want B to get destroyed,FLAG_ACTIVITY_REORDER_TO_FRONT only put A into top of ActivityStack without removing B. – kaushal trivedi Jul 06 '13 at 19:52
  • 1
    maybe you could use LocalBroadCastManager to send a separate intent to B to finish itself and then use Reorder_to_front? I could provide source code if this is something you are interested in – Ryan S Jul 06 '13 at 19:55
  • you mean we could use a separate BroadcastReceiver to just finish B.Is it so? – kaushal trivedi Jul 06 '13 at 19:57
  • correct, but it's actually very easy to do instead of having the BroadcastReceiver as a separate class, you can have it in the activity and dynamically declare it – Ryan S Jul 06 '13 at 19:59
  • sure i will look into that.Would like to see it. – kaushal trivedi Jul 06 '13 at 20:04
  • ok posted solution, goodluck :) – Ryan S Jul 06 '13 at 20:10

2 Answers2

4

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);
kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47
Ryan S
  • 4,549
  • 2
  • 21
  • 33
  • Hey,i don't find any class such as LocalBroadcastManager in my current android version,i am using android version 2.3.3,api level 10. – kaushal trivedi Jul 06 '13 at 20:21
  • you need to add the support library v4 http://developer.android.com/tools/extras/support-library.html – Ryan S Jul 07 '13 at 00:08
4

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

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80