Professional Documents
Culture Documents
What is an Activity?
HelloWorldActivity.java
public class HelloWorldActivity extends Activity { .... }
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>
Activity task
Intents!
Messaging facility for late run-time binding between components
Intent object
Action
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>
Activity re-use
Twitter Task
Twitter Task
Activity re-use
Gmail Task A
Browser
Task B
Gmail Task A
Activity re-use
Why different tasks?
Resuming an activity
Define how a new instance of an activity is associated with the current task
Demo App!!!
Search the market for: launchmode
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
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_SINGLE_TOP
Not recommended!
FLAG_ACTIVITY_NO_HISTORY
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_REORDER_TO_FRONT
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
The new activity is not kept in the list of recently launched activities
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
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
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
android:finishOnTaskLaunch
Shut down an instance of an activity when its task is resumed Trumps android:alwaysRetainTaskState
singleTask UI flow
singleInstance UI flow
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