Wednesday, December 30, 2015

Activities Android

- Container of UI

  Starting Activity
- startActivity / startActivityForResult / setResult/ onActivityResult

Implementing the lifecycle callbacks

Saving activity state
- onSaveInstanceState (Bundle)
- onCreate(Bundle) or onRestoreInstanceState(Bundle)

Handling configuration changes

Coordinating activities

   Tasks and Back Stack
- A task is a collection of activities that users interact with when performing a certain job.
- The activities are arranged in a stack (the back stack), in the order in which each activity is opened.
- When the user touches an icon in the application launcher that application's task comes to the foreground. If no task exists for the application (the application has not been used recently), then a new task is created and the "main" activity for that application opens as the root activity in the stack.
- When new activity is started, it's pushed in to back stack. The previous activity stopped and system retains it current user interface state. When user presses back key , activity which is in top of back stack is popped and destroyed by system and previous activity just resumes its state. 

Managing Tasks
- Application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack . one activity in your application might be instantiated multiple times. you can modify this behaviour if you do not want an activity to be instantiated more than once using below methods.
 
- standard ( default )

- singleTop 
- instantiated multiple times.
- launched into the task that called startActivity ((unless the Intent object contains a FLAG_ACTIVITY_NEW_TASK instruction)
- if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created. In other circumstances — for example, if an existing instance of the "singleTop" activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.
                                - Ex : Search Activity
 
- singleTask
- begins a task and always at the root of the activity stack
- the device can hold only one instance of the activity at a time — only one such task.
- allows other activities to be part of its task.

- singleInstance
- begins a task and always at the root of the activity stack
- the device can hold only one instance of the activity at a time — only one such task.
- permits no other activities to be part of its task.
                              - Ex: Activity for Launcher

Using Intent flags
- When starting an activity, you can modify the default association of an activity to its task by including flags in the intent  that you deliver to startActivity()
- FLAG_ACTIVITY_NEW_TASK
- FLAG_ACTIVITY_SINGLE_TOP
- FLAG_ACTIVITY_CLEAR_TOP


Note:
             - Similarly, if you navigate up (NavUtils.navigateUpTo(this, upIntent)) to an activity on the current stack, the behavior is determined by the parent activity's launch mode. If the parent activity has launch mode singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP), the parent is brought to the top of the stack, and its state is preserved. The navigation intent is received by the parent activity's onNewIntent() method. If the parent activity has launch mode standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent.
            - "singleTask" and "singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter.


Clearing the back stack 
- If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored. There are some activity attributes that you can use to modify this behavior

- alwaysRetainTaskState: retains all activities in its stack even after a long period, if this attribute is set to "true" in the root activity of a task.
- clearTaskOnLaunch: The user always returns to the task in its initial state, even after a leaving the task for only a moment.
- finishOnTaskLaunch: This attribute is like clearTaskOnLaunch, but it operates on a single activity, not an entire task.


LaunchMode Use case example
      http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

No comments:

Post a Comment