Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
Upcoming SlideShare
Loading in...5

Android | Android Activity Launch Modes and Tasks | Gonçalo Silva



2011-11-02 | 03:45 PM - 04:35 PM | ...

2011-11-02 | 03:45 PM - 04:35 PM |
Android Activities can possess one of four launch modes and one of many activity tasks. We will explore how your choices of launch modes and tasks affect your Applications back stack history and what will happen behind the scenes. After this talk you will confidently be able to wield the best launch modes for your apps activities in every situation!



Total Views
Views on SlideShare
Embed Views



1 Embed 29 29


Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Nice
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Android | Android Activity Launch Modes and Tasks | Gonçalo Silva Android | Android Activity Launch Modes and Tasks | Gonçalo Silva Presentation Transcript

  • Android activity launch modes and tasks Gonçalo Silva / Novoda
    • Co-organizer
    • ~1250 members
  • Activity launch modes and tasks..
  • What is an Activity?
  • Main building block of Android applications
  • public class HelloWorldActivity extends Activity { .... }
  • AndroidManifest.xml <? xml version = &quot;1.0&quot; encoding = &quot;utf-8&quot; ?> < manifest xmlns:android = &quot;; package = &quot;com.novoda.jax&quot; android:versionCode = &quot;1&quot; android:versionName = &quot;1.0&quot; > < application > < activity android:name = &quot;.HelloWorldActivity&quot; /> </ application > </ manifest >
  • Activity task Sequence of related activities a user follows
  • Activity stack / back stack
  • How are activites started?
  • Messaging facility for late run-time binding between components Intents!
  • Intent object
    • Action
    • Data – URI and/or MIME type
    • Category
    • Extras – key/value pairs
    • Component name
    • Flags
  • Explicit intent resolution Intent intent = new Intent( this , HelloWorldActivity. class ); startActivity(intent);
  • Implicit intent resolution Uri google = Uri. parse ( &quot;; ); Intent intent = new Intent( Intent. ACTION_VIEW , google); startActivity(intent);
  • Intent filter < activity android:name = &quot;.SimpleBrowser&quot; > < intent-filter > < action android:name = &quot;android.intent.action.VIEW&quot; /> < category android:name = &quot;android.intent.category.DEFAULT&quot; /> < data android:scheme = &quot;http&quot; /> </ intent-filter > </activity>
  • Intents are great! Loosely coupled activities Specify your behaviour as intentions
  • Activity re-use Starting an activity in a different application
  • Task Twitter
  • Task Twitter
  • Task Twitter Gmail
  • Activity re-use Some activities are started in a different task
  • Task A Gmail
  • Task A Gmail Task B Browser
  • Activity re-use Allows user to resume after leaving original task Why different tasks?
  • How can we resume an activity?
  • Resuming an activity
  • Resuming a “killed” activity
  • How can we choose a specific task behaviour for our activities?
  • Activity launch modes Define how a new instance of an activity is associated with the current task
  • Four launch modes
    • standard
    • singleTop
    • singleTask
    • singleInstance
  • Defined in the manifest < activity android:launchMode = &quot;singleTop&quot; android:name = &quot;.HelloWorldActivity&quot; />
  • Set as a flag in an intent Intent intent = new Intent( this , HelloWorldActivity. class ); intent.addFlags(Intent. FLAG_ACTIVITY_SINGLE_TOP ); startActivity(intent);
  • Demo App!!! Search the market for: launchmode
  • standard – default launch mode
  • singleTop A new activity X will not be started if there is an activity X on top of the stack onNewIntent() will be called instead of the normal activity lifecycle
  • singleTask Created at the root of a new task Only one instance of the activity can exist at a time onNewIntent() is called if an instance of the activity already exists
  • singleInstance Like singleTop but the only member of its task Any activities started will open in a separate task.
  • Examples of launchModes
    • singleTop: Dashboard, Search
    • singleTask: Browser, Google Maps
    • singleInstance: Home screen
  • Intent flags and launchModes
  • FLAG_ACTIVITY_MULTIPLE_TASK FLAG_ACTIVITY_NEW_TASK Force an activity to a new task Not recommended!
  • FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS The new activity is not kept in the list of recently launched activities
  • Other manifest attributes to help deal with activities and tasks
  • android:taskAffinity
    • Useful to ensure certain activities are always in the same task
    • By default, all activities in an application have the same affinity
    • singleTask Gotcha!!!
  • android:allowTaskReparenting
    • Next time an activity comes to the front, move it to a task it has an affinity for
    • Eg: email and browser
  • android:clearTaskOnLaunch
    • Clear all activities from the task except the root, when launching the app from the home screen
    • Only honoured for activities that start a new task
    • Can be combined with “allowTaskReparenting”
  • android:alwaysRetainTaskState
    • The state of the task is mantained by the system, even after a long period of time
    • Only honoured for activities that start a new task
    • Eg: browser tabs
  • android:finishOnTaskLaunch
    • Shut down an instance of an activity when its task is resumed
    • Trumps android:alwaysRetainTaskState
  • singleTask and singleInstance change the UI flow Use with caution!
  • singleTask UI flow
  • singleInstance UI flow
  • Two launcher icons
  • Provide a launcher icon for each singleTask / singleInstance activity
  • Another disadvantage: startActivityForResult()
  • Recommended launchModes: standard and singleTop with intent flags combinations will suit most use cases
  • To recap:
    • Activities and how they live in tasks
    • Intents and re-using activities in other apps
    • Activity lifecycle and the four launchmodes
    • Intent flags and manifest attributes
    • singleTask and singleInstance disadvantages
  • Thank you for listening! Any Questions?