1page.title=Tasks and Back Stack 2parent.title=Activities 3parent.link=activities.html 4@jd:body 5 6<div id="qv-wrapper"> 7<div id="qv"> 8<h2>Quickview</h2> 9<ul> 10 <li>All activities belong to a task</li> 11 <li>A task contains a collection of activities in the order in which the user interacts with 12them</li> 13 <li>Tasks can move to the background and retain the state of each activity in order for users 14to perform other tasks without losing their work</li> 15</ul> 16 17<h2>In this document</h2> 18<ol> 19<li><a href="#ActivityState">Saving Activity State</a></li></li> 20<li><a href="#ManagingTasks">Managing Tasks</a> 21 <ol> 22 <li><a href="#TaskLaunchModes">Defining launch modes</a></li> 23 <li><a href="#Affinities">Handling affinities</a></li> 24 <li><a href="#Clearing">Clearing the back stack</a></li> 25 <li><a href="#Starting">Starting a task</a></li> 26 </ol> 27</li> 28</ol> 29 30<h2>Articles</h2> 31<ol> 32 <li><a href="{@docRoot}resources/articles/multitasking-android-way.html">Multitasking the Android Way</a></li> 33</ol> 34 35<h2>See also</h2> 36<ol> 37 <li><a><a href="{@docRoot}videos/index.html#v=fL6gSd4ugSI">Application Lifecycle video</a></li> 38 <li><a 39href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>} manifest 40element</a></li> 41</ol> 42</div> 43</div> 44 45 46<p>An application usually contains multiple <a 47href="{@docRoot}guide/topics/fundamentals/activities.html">activities</a>. Each activity 48should be designed around a specific kind of action the user can perform and can start other 49activities. For example, an email application might have one activity to show a list of new email. 50When the user selects an email, a new activity opens to view that email.</p> 51 52<p>An activity can even start activities that exist in other applications on the device. For 53example, if your application wants to send an email, you can define an intent to perform a "send" 54action and include some data, such as an email address and a message. An activity from another 55application that declares itself to handle this kind of intent then opens. In this case, the intent 56is to send an email, so an email application's "compose" activity starts (if multiple activities 57support the same intent, then the system lets the user select which one to use). When the email is 58sent, your activity resumes and it seems as if the email activity was part of your application. Even 59though the activities may be from different applications, Android maintains this seamless user 60experience by keeping both activities in the same <em>task</em>.</p> 61 62<p>A task is a collection of activities that users interact with 63when performing a certain job. The activities are arranged in a stack (the "back stack"), in the 64order in which each activity is opened.</p> 65 66<!-- SAVE FOR WHEN THE FRAGMENT DOC IS ADDED 67<div class="sidebox-wrapper"> 68<div class="sidebox"> 69<h3>Adding fragments to a task's back stack</h3> 70 71<p>Your activity can also include {@link android.app.Fragment}s to the back stack. For example, 72suppose you have a two-pane layout using fragments, one of which is a list view (fragment A) and the 73other being a layout to display an item from the list (fragment B). When the user selects an item 74from the list, fragment B is replaced by a new fragment (fragment C). In this case, it might be 75desireable for the user to navigate back to reveal fragment B, using the BACK key.</p> 76<p>In order to add fragment B to the back stack so that this is possible, you must call {@link 77android.app.FragmentTransaction#addToBackStack addToBackStack()} before you {@link 78android.app.FragmentTransaction#commit()} the transaction that replaces fragment B with fragment 79C.</p> 80<p>For more information about using fragments and adding them to the back stack, see the {@link 81android.app.Fragment} class documentation.</p> 82 83</div> 84</div> 85--> 86 87<p>The device Home screen is the starting place for most tasks. When the user touches an icon in the 88application 89launcher (or a shortcut on the Home screen), that application's task comes to the foreground. If no 90task exists for the application (the application has not been used recently), then a new task 91is created and the "main" activity for that application opens as the root activity in the stack.</p> 92 93<p>When the current activity starts another, the new activity is pushed on the top of the stack and 94takes focus. The previous activity remains in the stack, but is stopped. When an activity 95stops, the system retains the current state of its user interface. When the user presses the BACK 96key, the current activity is popped from the top of the stack (the activity is destroyed) and the 97previous activity resumes (the previous state of its UI is restored). Activities in the stack are 98never rearranged, only pushed and popped from the stack—pushed onto the stack when started by 99the current activity and popped off when the user leaves it using the BACK key. As such, the back 100stack operates as a "last in, first out" object structure. Figure 1 visualizes 101this behavior with a timeline showing the progress between activities along with the current back 102stack at each point in time.</p> 103 104<img src="{@docRoot}images/fundamentals/diagram_backstack.png" alt="" /> 105<p class="img-caption"><strong>Figure 1.</strong> A representation of how each new activity in a 106task adds an item to the back stack. When the user presses the BACK key, the current activity is 107destroyed and the previous activity resumes.</p> 108 109 110<p>If the user continues to press BACK, then each activity in the stack is popped off to reveal the 111previous one, until the user returns to the Home screen (or to whichever activity was running when 112the task began). When all activities are removed from the stack, the task no longer exists.</p> 113 114<div class="figure" style="width:369px"> 115<img src="{@docRoot}images/fundamentals/diagram_multitasking.png" alt="" /> <p 116class="img-caption"><strong>Figure 2.</strong> Two tasks: Task A is in the background, waiting 117to be resumed, while Task B receives user interaction in the foreground.</p> 118</div> 119<div class="figure" style="width:178px"> 120 <img src="{@docRoot}images/fundamentals/diagram_multiple_instances.png" alt="" /> <p 121class="img-caption"><strong>Figure 3.</strong> A single activity is instantiated multiple times.</p> 122</div> 123 124<p>A task is a cohesive unit that can move to the "background" when users begin a new task or go 125to the Home screen, via the HOME key. While in the background, all the activities in the task are 126stopped, but the back stack for the task remains intact—the task has simply lost focus while 127another task takes place, as shown in figure 2. A task can then return to the "foreground" so users 128can pick up where they left off. Suppose, for example, that the current task (Task A) has three 129activities in its stack—two under the current activity. The user presses the HOME key, then 130starts a new application from the application launcher. When the Home screen appears, Task A goes 131into the background. When the new application starts, the system starts a task for that application 132(Task B) with its own stack of activities. After interacting with 133that application, the user returns Home again and selects the application that originally 134started Task A. Now, Task A comes to the 135foreground—all three activities in its stack are intact and the activity at the top of the 136stack resumes. At 137this point, the user can also switch back to Task B by going Home and selecting the application icon 138that started that task (or by touching and holding the HOME key to reveal recent tasks and selecting 139one). This is an example of multitasking on Android.</p> 140 141<p class="note"><strong>Note:</strong> Multiple tasks can be held in the background at once. 142However, if the user is running many background tasks at the same time, the system might begin 143destroying background activities in order to recover memory, causing the activity states to be lost. 144See the following section about <a href="#ActivityState">Activity state</a>.</p> 145 146<p>Because the activities in the back stack are never rearranged, if your application allows 147users to start a particular activity from more than one activity, a new instance of 148that activity is created and popped onto the stack (rather than bringing any previous instance of 149the activity to the top). As such, one activity in your application might be instantiated multiple 150times (even from different tasks), as shown in figure 3. As such, if the user navigates backward 151using the BACK key, each instance of the activity is revealed in the order they were opened (each 152with their own UI state). However, you can modify this behavior if you do not want an activity to be 153instantiated more than once. How to do so is discussed in the later section about <a 154href="#ManagingTasks">Managing Tasks</a>.</p> 155 156 157<p>To summarize the default behavior for activities and tasks:</p> 158 159<ul> 160 <li>When Activity A starts Activity B, Activity A is stopped, but the system retains its state 161(such as scroll position and text entered into forms). 162If the user presses the BACK key while in Activity B, Activity A resumes with its state 163restored.</li> 164 <li>When the user leaves a task by pressing the HOME key, the current activity is stopped and 165its task goes into the background. The system retains the state of every activity in the task. If 166the user later resumes the task by selecting the launcher icon that began the task, the task comes 167to the foreground and resumes the activity at the top of the stack.</li> 168 <li>If the user presses the BACK key, the current activity is popped from the stack and 169destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system 170<em>does not</em> retain the activity's state.</li> 171 <li>Activities can be instantiated multiple times, even from other tasks.</li> 172</ul> 173 174 175<h2 id="ActivityState">Saving Activity State</h2> 176 177<p>As discussed above, the system's default behavior preserves the state of an activity when it is 178stopped. This way, when users navigate back to a previous activity, its user interface appears 179the way they left it. However, you can—and <strong>should</strong>—proactively retain 180the state of your activities using callback methods, in case the activity is destroyed and must 181be recreated.</p> 182 183<p>When the system stops one of your activities (such as when a new activity starts or the task 184moves to the background), the system might destroy that activity completely if it needs to recover 185system memory. When this happens, information about the activity state is lost. If this happens, the 186system still 187knows that the activity has a place in the back stack, but when the activity is brought to the 188top of the stack the system must recreate it (rather than resume it). In order to 189avoid losing the user's work, you should proactively retain it by implementing the {@link 190android.app.Activity#onSaveInstanceState onSaveInstanceState()} callback 191methods in your activity.</p> 192 193<p>For more information about how to save your activity state, see the <a 194href="{@docRoot}guide/topics/fundamentals/activities.html#SavingActivityState">Activities</a> 195document.</p> 196 197 198 199<h2 id="ManagingTasks">Managing Tasks</h2> 200 201<p>The way Android manages tasks and the back stack, as described above—by placing all 202activities started in succession in the same task and in a "last in, first out" stack—works 203great for most applications and you shouldn't have to worry about how your activities are associated 204with tasks or how they exist in the back stack. However, you might decide that you want to interrupt 205the normal behavior. Perhaps you want an activity in your application to begin a new task when it is 206started (instead of being placed within the current task); or, when you start an activity, you want 207to bring forward an existing instance of it (instead of creating a new 208instance on top of the back stack); or, you want your back stack to be cleared of all 209activities except for the root activity when the user leaves the task.</p> 210 211<p>You can do these things and more, with attributes in the 212<a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code 213<activity>}</a> manifest element and with flags in the intent that you pass to {@link 214android.app.Activity#startActivity startActivity()}.</p> 215 216<p>In this regard, the the principal <a 217href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> 218attributes you can use are:</p> 219 220<ul class="nolist"> 221 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#aff">{@code 222taskAffinity}</a></li> 223 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code 224launchMode}</a></li> 225 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#reparent">{@code 226allowTaskReparenting}</a></li> 227 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#clear">{@code 228clearTaskOnLaunch}</a></li> 229 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#always">{@code 230alwaysRetainTaskState}</a></li> 231 <li><a href="{@docRoot}guide/topics/manifest/activity-element.html#finish">{@code 232finishOnTaskLaunch}</a></li> 233</ul> 234 235<p>And the principal intent flags you can use are:</p> 236 237<ul class="nolist"> 238 <li>{@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK}</li> 239 <li>{@link android.content.Intent#FLAG_ACTIVITY_CLEAR_TOP}</li> 240 <li>{@link android.content.Intent#FLAG_ACTIVITY_SINGLE_TOP}</li> 241</ul> 242 243<p>In the following sections, you'll see how you can use these manifest attributes and intent 244flags to define how activities are associated with tasks and how the behave in the back stack.</p> 245 246 247<p class="caution"><strong>Caution:</strong> Most applications should not interrupt the default 248behavior for activities and tasks. If you determine that it's necessary for your activity to modify 249the default behaviors, use caution and be sure to test the usability of the activity during 250launch and when navigating back to it from other activities and tasks with the BACK key. Be sure 251to test for navigation behaviors that might conflict with the user's expected behavior.</p> 252 253 254<h3 id="TaskLaunchModes">Defining launch modes</h3> 255 256<p>Launch modes allow you to define how a new instance of an activity is associated with the 257current task. You can define different launch modes in two ways:</p> 258<ul class="nolist"> 259 <li><a href="#ManifestForTasks">Using the manifest file</a> 260 <p>When you declare an activity in your manifest file, you can specify how the activity 261should associate with tasks when it starts.</li> 262 <li><a href="#IntentFlagsForTasks">Using Intent flags</a> 263 <p>When you call {@link android.app.Activity#startActivity startActivity()}, 264you can include a flag in the {@link android.content.Intent} that declares how (or 265whether) the new activity should associate with the current task.</p></li> 266</ul> 267 268<p>As such, if Activity A starts Activity B, Activity B can define in its manifest how it 269should associate with the current task (if at all) and Activity A can also request how Activity 270B should associate with current task. If both activities define how Activity B 271should associate with a task, then Activity A's request (as defined in the intent) is honored 272over Activity B's request (as defined in its manifest).</p> 273 274<p class="note"><strong>Note:</strong> Some the launch modes available in the manifest 275are not available as flags for an intent and, likewise, some launch modes available as flags 276for an intent cannot be defined in the manifest.</p> 277 278 279<h4 id="ManifestForTasks">Using the manifest file</h4> 280 281<p>When declaring an activity in your manifest file, you can specify how the activity should 282associate with a task using the <a 283href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> 284element's <a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code 285launchMode}</a> attribute.</p> 286 287<p>The <a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code 288launchMode}</a> attribute specifies an instruction on how the activity should be launched into a 289task. There are four different launch modes you can assign to the 290<code><a href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">launchMode</a></code> 291attribute:</p> 292 293<dl> 294<dt>{@code "standard"} (the default mode)</dt> 295 <dd>Default. The system creates a new instance of the activity in the task from 296which it was started and routes the intent to it. The activity can be instantiated multiple times, 297each instance can belong to different tasks, and one task can have multiple instances.</dd> 298<dt>{@code "singleTop"}</dt> 299 <dd>If an instance of the activity already exists at the top of the current task, the system 300routes the intent to that instance through a call to its {@link 301android.app.Activity#onNewIntent onNewIntent()} method, rather than creating a new instance of the 302activity. The activity can be instantiated multiple times, each instance can 303belong to different tasks, and one task can have multiple instances (but only if the the 304activity at the top of the back stack is <em>not</em> an existing instance of the activity). 305 <p>For example, suppose a task's back stack consists of root activity A with activities B, C, 306and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. 307If D has the default {@code "standard"} launch mode, a new instance of the class is launched and the 308stack becomes A-B-C-D-D. However, if D's launch mode is {@code "singleTop"}, the existing instance 309of D receives the intent through {@link 310android.app.Activity#onNewIntent onNewIntent()}, because it's at the top of the stack—the 311stack remains A-B-C-D. However, if an intent arrives for an activity of type B, then a new 312instance of B is added to the stack, even if its launch mode is {@code "singleTop"}.</p> 313 <p class="note"><strong>Note:</strong> When a new instance of an activity is created, 314the user can press the BACK key to return to the previous activity. But when an existing instance of 315an activity handles a new intent, the user cannot press the BACK key to return to the state of 316the activity before the new intent arrived in {@link android.app.Activity#onNewIntent 317onNewIntent()}.</p> 318</dd> 319 320<dt>{@code "singleTask"}</dt> 321 <dd>The system creates a new task and instantiates the activity at the root of the new task. 322However, if an instance of the activity already exists in a separate task, the system routes the 323intent to the existing instance through a call to its {@link 324android.app.Activity#onNewIntent onNewIntent()} method, rather than creating a new instance. Only 325one instance of the activity can exist at a time. 326 <p class="note"><strong>Note:</strong> Although the activity starts in a new task, the 327BACK key still returns the user to the previous activity.</p></dd> 328<dt>{@code "singleInstance"}.</dt> 329 <dd>Same as {@code "singleTask"}, except that the system doesn't launch any other activities into 330the task holding the instance. The activity is always the single and only member of its task; 331any activities started by this one open in a separate task.</dd> 332</dl> 333 334 335<p>As another example, the Android Browser application declares that the web browser activity should 336always open in its own task—by specifying the {@code singleTask} launch mode in the <a 337href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> element. 338This means that if your application issues an 339intent to open the Android Browser, its activity is <em>not</em> placed in the same 340task as your application. Instead, either a new task starts for the Browser or, if the Browser 341already has a task running in the background, that task is brought forward to handle the new 342intent.</p> 343 344<p>Regardless of whether an activity starts in a new task or in the same task as the activity that 345started it, the BACK key always takes the user to the previous activity. However, if you 346start an activity from your task (Task A) that specifies the {@code singleTask} launch mode, then 347that activity might have an instance in the background that belongs to a task with its own back 348stack (Task B). In this 349case, when Task B is brought forward to handle a new intent, the BACK key first navigates 350backward through the activities in Task B before returning to 351the top-most activity in Task A. Figure 4 visualizes this type of scenario.</p> 352 353<img src="{@docRoot}images/fundamentals/diagram_backstack_singletask_multiactivity.png" alt="" /> 354<p class="img-caption"><strong>Figure 4.</strong> A representation of how an activity with 355launch mode "singleTask" is added to the back stack. If the activity is already a part of a 356background task with its own back stack (Task B), then the entire back stack also comes 357forward, on top of the current task (Task A).</p> 358 359<p>For more information about using launch modes in the manifest file, see the 360<code><a href="{@docRoot}guide/topics/manifest/activity-element.html"><activity></a></code> 361element documentation, where the {@code launchMode} attribute and the accepted values are 362discussed more.</p> 363 364<p class="note"><strong>Note:</strong> The behaviors that you specify for your activity with the <a 365href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code launchMode}</a> attribute 366can be overridden by flags included with the intent that start your activity, as discussed in the 367next section.</p> 368 369 370 371<h4 id="#IntentFlagsForTasks">Using Intent flags</h4> 372 373<p>When starting an activity, you can modify the default association of an activity to its task 374by including flags in the intent that you deliver to {@link 375android.app.Activity#startActivity startActivity()}. The flags you can use to modify the 376default behavior are:</p> 377 378<p> 379 <dt>{@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK}</dt> 380 <dd>Start the activity in a new task. If a task is already running for the activity you are now 381starting, that task is brought to the foreground with its last state restored and the activity 382receives the new intent in {@link android.app.Activity#onNewIntent onNewIntent()}. 383 <p>This produces the same behavior as the {@code "singleTask"} <a 384href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code launchMode}</a> value, 385discussed in the previous section.</p></dd> 386 <dt>{@link android.content.Intent#FLAG_ACTIVITY_SINGLE_TOP}</dt> 387 <dd>If the activity being started is the current activity (at the top of the back stack), then 388the existing instance receives a call to {@link android.app.Activity#onNewIntent onNewIntent()}, 389instead of creating a new instance of the activity. 390 <p>This produces the same behavior as the {@code "singleTop"} <a 391href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code launchMode}</a> value, 392discussed in the previous section.</p></dd> 393 <dt>{@link android.content.Intent#FLAG_ACTIVITY_CLEAR_TOP}</dt> 394 <dd>If the activity being started is already running in the current task, then instead 395of launching a new instance of that activity, all of the other activities on top of it are 396destroyed and this intent is delivered to the resumed instance of the activity (now on top), 397through {@link android.app.Activity#onNewIntent onNewIntent()}). 398 <p>There is no value for the <a 399href="{@docRoot}guide/topics/manifest/activity-element.html#lmode">{@code launchMode}</a> 400attribute that produces this behavior.</p> 401 <p>{@code FLAG_ACTIVITY_CLEAR_TOP} is most often used in conjunction with {@code 402FLAG_ACTIVITY_NEW_TASK}. When used together, these flags are a way of locating an existing activity 403in another task and putting it in a position where it can respond to the intent. </p> 404 <p class="note"><strong>Note:</strong> If the launch mode of the designated activity is {@code 405"standard"}, it too is removed from the stack and a new instance is launched in its place to handle 406the incoming intent. That's because a new instance is always created for a new intent when the 407launch mode is {@code "standard"}. </p> 408</dd> 409</dl> 410 411 412 413 414 415<h3 id="Affinities">Handling affinities</h3> 416 417<p>The <em>affinity</em> indicates which task an activity prefers to belong to. By default, all the 418activities from the same application have an affinity for each other. So, by default, all 419activities in the same application prefer to be in the same task. However, you can modify 420the default affinity for an activity. Activities defined in 421different applications can share an affinity, or activities defined in the same application can be 422assigned different task affinities.</p> 423 424<p>You can modify the affinity for any given activity with the <a 425href="{@docRoot}guide/topics/manifest/activity-element.html#aff">{@code taskAffinity}</a> attribute 426of the <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code <activity>}</a> 427element.</p> 428 429<p>The <a 430href="{@docRoot}guide/topics/manifest/activity-element.html#aff">{@code taskAffinity}</a> 431attribute takes a string value, which must be unique from the default package name 432declared in the <a href="{@docRoot}guide/topics/manifest/manifest-element.html">{@code 433<manifest>}</a> element, because the system uses that name to identify the default task 434affinity for the application.</p> 435 436<p>The affinity comes into play in two circumstances:</p> 437<ul> 438 <li>When the intent that launches an activity contains the {@link 439android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag. 440 441<p>A new activity is, by default, launched into the task of the activity 442that called {@link android.app.Activity#startActivity startActivity()}. It's pushed onto the same 443back stack as the caller. However, if the intent passed to {@link 444android.app.Activity#startActivity startActivity()} contains the {@link 445android.content.Intent#FLAG_ACTIVITY_NEW_TASK} 446flag, the system looks for a different task to house the new activity. Often, it's a new task. 447However, it doesn't have to be. If there's already an existing task with the same affinity as the 448new activity, the activity is launched into that task. If not, it begins a new task.</p> 449 450<p>If this flag causes an activity to begin a new task and the user presses the HOME key to leave 451it, there must be some way for the user to navigate back to the task. Some entities (such as the 452notification manager) always start activities in an external task, never as part of their own, so 453they always put {@code FLAG_ACTIVITY_NEW_TASK} in the intents they pass to {@link 454android.app.Activity#startActivity startActivity()}. If you have an activity that can be invoked by 455an external entity that might use this flag, take care that the user has a independent way to get 456back to the task that's started, such as with a launcher icon (the root activity of the task 457has a {@link android.content.Intent#CATEGORY_LAUNCHER} intent filter; see the <a 458href="#Starting">Starting a task</a> section below).</p> 459</li> 460 461 <li>When an activity has its <a 462href="{@docRoot}guide/topics/manifest/activity-element.html#reparent">{@code 463allowTaskReparenting}</a> attribute set to {@code "true"}. 464 <p>In this case, the activity can move from the task it starts to the task it has an affinity 465for, when that task comes to the foreground.</p> 466 <p>For example, suppose that an activity that reports weather conditions in selected cities is 467defined as part of a travel application. It has the same affinity as other activities in the same 468application (the default application affinity) and it allows re-parenting with this attribute. 469When one of your activities starts the weather reporter activity, it initially belongs to the same 470task as your activity. However, when the travel application's task comes to the foreground, the 471weather reporter activity is reassigned to that task and displayed within it.</p> 472</li> 473</ul> 474 475<p class="note"><strong>Tip:</strong> If an {@code .apk} file contains more than one "application" 476from the user's point of view, you probably want to use the <a 477href="{@docRoot}guide/topics/manifest/activity-element.html#aff">{@code taskAffinity}</a> 478attribute to assign different affinities to the activities associated with each "application".</p> 479 480 481 482<h3 id="Clearing">Clearing the back stack</h3> 483 484<p>If the user leaves a task for a long time, the system clears the task of all activities except 485the root activity. When the user returns to the task again, only the root activity is restored. 486The system behaves this way, because, after an extended amount of time, users likely have abandoned 487what they were doing before and are returning to the task to begin something new. </p> 488 489<p>There are some activity attributes that you can use to modify this behavior: </p> 490 491<dl> 492<dt><code><a 493href="{@docRoot}guide/topics/manifest/activity-element.html#always">alwaysRetainTaskState</a></code> 494</dt> 495<dd>If this attribute is set to {@code "true"} in the root activity of a task, 496the default behavior just described does not happen. 497The task retains all activities in its stack even after a long period.</dd> 498 499<dt><code><a 500href="{@docRoot}guide/topics/manifest/activity-element.html#clear">clearTaskOnLaunch</a></code></dt> 501<dd>If this attribute is set to {@code "true"} in the root activity of a task, 502the stack is cleared down to the root activity whenever the user leaves the task 503and returns to it. In other words, it's the opposite of <a 504href="{@docRoot}guide/topics/manifest/activity-element.html#always">{@code 505alwaysRetainTaskState}</a>. The user always returns to the task in its 506initial state, even after a leaving the task for only a moment.</dd> 507 508<dt><code><a 509href="{@docRoot}guide/topics/manifest/activity-element.html#finish">finishOnTaskLaunch</a></code> 510</dt> 511<dd>This attribute is like <a 512href="{@docRoot}guide/topics/manifest/activity-element.html#clear">{@code clearTaskOnLaunch}</a>, 513but it operates on a 514single activity, not an entire task. It can also cause any activity to go 515away, including the root activity. When it's set to {@code "true"}, the 516activity remains part of the task only for the current session. If the user 517leaves and then returns to the task, it is no longer present.</dd> 518</dl> 519 520 521 522 523<h3 id="Starting">Starting a task</h3> 524 525<p>You can set up an activity as the entry point for a task by giving it an intent filter with 526{@code "android.intent.action.MAIN"} as the specified action and {@code 527"android.intent.category.LAUNCHER"} as the specified category. For example:</p> 528 529<pre> 530<activity ... > 531 <intent-filter ... > 532 <action android:name="android.intent.action.MAIN" /> 533 <category android:name="android.intent.category.LAUNCHER" /> 534 </intent-filter> 535 ... 536</activity> 537</pre> 538 539<p>An intent filter of this kind causes an icon and label for the 540activity to be displayed in the application launcher, giving users a way to launch the activity and 541to return to the task that it creates any time after it has been launched. 542</p> 543 544<p>This second ability is important: Users must be able to leave a task and then come back to it 545later using this activity launcher. For this reason, the two <a href="#LaunchModes">launch 546modes</a> that mark activities as always initiating a task, {@code "singleTask"} and "{@code 547"singleInstance"}, should be used only when the activity has an {@link 548android.content.Intent#ACTION_MAIN} 549and a {@link android.content.Intent#CATEGORY_LAUNCHER} 550filter. Imagine, for example, what could happen if the filter is missing: An intent launches a 551{@code "singleTask"} activity, initiating a new task, and the user spends some time working in 552that task. The user then presses the HOME key. The task is now sent to the background and not 553visible. Because it is not represented in the application launcher, the user has no way to return to 554the task. 555</p> 556 557<p>For those cases where you don't want the user to be able to return to an activity, set the 558 <code><a 559href="{@docRoot}guide/topics/manifest/activity-element.html"><activity></a></code> element's 560<a href="{@docRoot}guide/topics/manifest/activity-element.html#finish">{@code 561finishOnTaskLaunch}</a> to {@code "true"} (see <a 562href="#Clearing">Clearing the stack</a>).</p> 563 564 565 566<!-- 567<h2>Beginner's Path</h2> 568 569<p>For more information about how to use intents to 570activate other application components and publish the intents to which your components 571respond, continue with the <b><a 572href="{@docRoot}guide/topics/intents/intents-filters.html">Intents and Intent 573Filters</a></b> document.</p> 574--> 575