Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FLAG_ACTIVITY_CLEAR_TOP in Android

Tags:

android

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.

like image 414
lomza Avatar asked Sep 12 '11 08:09

lomza


People also ask

What is Flag_activity_clear_top?

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 back stack in Android?

A task is a collection of activities that users interact with when trying to do something in your app. These activities are arranged in a stack—the back stack—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages.

How do you use flags on Android?

Use Intent Flags Intents are used to launch activities on Android. You can set flags that control the task that will contain the activity. Flags exist to create a new activity, use an existing activity, or bring an existing instance of an activity to the front.

What is Flag_activity_new_task?

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.


1 Answers

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.

like image 140
Deva Avatar answered Sep 22 '22 00:09

Deva