AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.novoda.jax" android:versionCode="1" android:versionName="1.0" >
<application >
<activity android:name=".HelloWorldActivity" />
</application>
</manifest>
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("http://www.google.com");
Intent intent = new Intent( Intent.ACTION_VIEW, google);
startActivity(intent);
Intent filter
<activity android:name=".SimpleBrowser" >
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
Defined in the manifest
<activity android:launchMode="singleTop" android:name=".HelloWorldActivity" />
Set as a flag in an intent
Intent intent = new Intent(this, HelloWorldActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
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
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
The new activity is not kept in the list of recently launched activities
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
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