59

Can somebody explain me in a really simple way what does FLAG_ACTIVITY_CLEAR_TOP mean? I know there were a lot of questions about it, but none of the answers satisfied me. Can somebody also give an example where this flag is useful? Thanks.

Mathias Müller
  • 20,222
  • 13
  • 53
  • 68
lomza
  • 8,576
  • 13
  • 63
  • 82
  • See this link http://stackoverflow.com/questions/4342761/how-do-you-use-intent-flag-activity-clear-top-to-clear-the-activity-stack check also this link that explain....................... http://mgmblog.com/2010/07/22/go-home-feature-with-flag_activity_clear_top/ – Samir Mangroliya Sep 12 '11 at 09:19
  • http://stackoverflow.com/a/23718678/513413 – Hesam Dec 18 '14 at 06:22

1 Answers1

92

Please check the below link for the details of the same:

http://developer.android.com/reference/android/content/Intent.html

What it means is: let's say you have 4 activities, A, B, C, and D, and the flow is

A -> B -> C -> D

and now when you are on D you want to start activity B (from the stack and not a new instance) then you can use this intent flag. Also what it does is remove all the other activities on top of B (here C and D).

A realtime example would be an email app with activities ReadMailInInbox -> OpenMailFullScreen -> ReplyMail once you reply to your mail you wont want to go back to OpenMailFullScreen rather you would want your ReadMailInInbox activity to come on top so you can start this activity by passing an intent with the flag set as FLAG_ACTIVITY_CLEAR_TOP.

Hope this helps.

dsh
  • 11,380
  • 3
  • 27
  • 48
Deva
  • 3,809
  • 22
  • 36
  • 22
    @Deva, So according to your example, after getting back to already exists instance of activity B, as you said; activities C and D should have now been cleared from the stack. but as I've tested if I hit back button on activity B, activity D is still shown. why? – anonim Sep 07 '12 at 08:02
  • 2
    @anonim, possible problem can be in `launchMode`. You need to use `singleTask` instead of `singleInstance`. Perfect explanation is here http://stackoverflow.com/a/36111557/1219012 – Artem Mostyaev Mar 20 '16 at 08:18
  • I tested this scenario it is working fine but I did't get it why onCreate of B is calling again ? – Dhara Patel Jul 19 '19 at 09:52