Lecture 3 getting active through activities
-
Upload
ahsanul-karim -
Category
Technology
-
view
363 -
download
0
description
Transcript of Lecture 3 getting active through activities
Lecture 3: Getting Active Through Activities and Intents
Ahsanul [email protected]
http://droidtraining.wordpress.com
Android Application Development
1. Android App Anatomy2. What is Activity3. Activity Lifecycle4. Handling Runtime Changes5. Multiple Activities7. Activity Stack8. Intents
1. Android App Anatomy
2. What is Activity
3. Activity Lifecycle
Getting Active Through Activities Today We'll Discuss...
4. Handling Runtime Changes
5. Multiple Activities
6. Activity Stack
There are 4 types of application components/building blocks:
Getting Active Through Activities Application Anatomy
Activities1. Activity provides user interface2. Usually represents a single screen3. Can contain one or more views4. Extends the Activity base class
Services
1. No user interface2. Runs in background3. Extends the Service base class
BroadcastReceiver1. Receives and Reacts to broadcast Intents2. No UI but can start an Activity3. Extends the BroadcastReceiver base class
ContentProviders1. Makes application data available to other apps [data sharing]2. Uses SQLite database as storage3. Extends the ContentProvider base class
Getting Active Through Activities What is Activity... Activity
Activity provides a user generally with an interactive screen to do something like: Dialing the phone, View a map or list of something etc.
1. An application usually consists of multiple activities.2. Typically, one activity in an application is specified as the "main" activity.
The <action> element specifies that this is the "main" entry point to the application. The <category> element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).
Activity Lifecycle
Getting Active Through Activities Activity Lifecycle [1]
During the life of an activity, the system calls a core set of lifecycle methods
We'll see a demo on this...
Getting Active Through Activities Activity Lifecycle [2]
Depending on the complexity of the activity, we may not need to implement all the lifecycle methods. Implementing activity lifecycle methods properly ensures the app behaves well in several ways, including that it:
1. Does not crash if the user receives a phone call or switches to another app while using the app.
2. Does not consume valuable system resources when the user is not actively using it.
3. Does not lose the user's progress if they leave the app and return to it later.
4. Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation.
Getting Active Through Activities Activity Lifecycle [3]
Starting an Activity
three main callbacks that called in sequence when creating new instance of activity: onCreate(), onStart(), and onResume(). After sequence of callbacks complete, activity reaches the Resumed state where users can interact with the activity.
Getting Active Through Activities Activity Lifecycle [5]
Pausing & Resuming an Activity
When a semi-transparent activity obscures your activity, the system calls onPause() and the activity waits in the Paused state (1). If the user returns to the activity while it's still paused, the system calls onResume()
Getting Active Through Activities Activity Lifecycle [6]
Pausing: onPause()
We should usually use the onPause() callback to:
Operations in onPause() must be kept simple in order to allow for a speedy transition to the user's next destination.
1. Stop animations or other ongoing actions that could consume CPU.
2. Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
3. Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life.
Getting Active Through Activities Activity Lifecycle [7]
Stopping & Restarting an Activity
When the user leaves your activity, the system calls onStop() to stop the activity (1). If the user returns while the activity is stopped, the system calls onRestart() (2), quickly followed by onStart() (3) and onResume() (4).
Getting Active Through Activities Activity Lifecycle [8]
Stopping & Restarting an Activity
There are a few of key scenarios in which activity is stopped and restarted:
1. User opens the Recent Apps window and switches from the app to another app. If the user returns to the app from the Home screen launcher icon or the Recent Apps window, the activity restarts.
2. When a new Activity is launched. The current activity is stopped when the second activity is created. If the user then presses the Back button, the first activity is restarted.
3. The user receives a phone call while using your app on his or her phone.
We should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database.
Getting Active Through Activities Activity Lifecycle [9]
Recreating an Activity
As the system begins to stop your activity, it calls onSaveInstanceState() (1) so you can specify additional state data you'd like to save in case the Activity instance must be recreated. If the activity is destroyed and the same instance must be recreated, the system passes the state data defined at (1) to both the onCreate() method (2) and the onRestoreInstanceState() method (3).
Getting Active Through Activities Runtime Changes...
Handling Runtime Changes
Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, onDestroy()is called, followed by onCreate().
The restart behavior is designed to help the application adapt to new configurations by automatically reloading the application with alternative resources that match the new device configuration.
Often we have the following 2 options:
Retain an object at configuration changeAllow the activity to restart when a configuration changes, but carry a stateful Object to the new instance of activity.
Handle the configuration change yourselfPrevent the system from restarting activity but receive a callback when the config changes..
Getting Active Through Activities Multiple Activities..
1. Adding New Activity
2. Passing Data Between Activities Using Intents
3. Passing Objects Between Activities
4. Starting New Activity For Result
5. Starting Activities of Other Applications
Getting Active Through Activities Activity Stack [1]
A representation of how each new activity in a task adds an item to the back stack. When the user presses the Back button, the current activity is destroyed and the previous activity resumes.
Getting Active Through Activities Activity Stack [2]
1. When Activity A starts Activity B, Activity A is stopped, but the system retains its state (such as scroll position and text entered into forms). If the user presses the Back button while in Activity B, Activity A resumes with its state restored.
2. When the user leaves a task by pressing the Home button, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack.
3. If the user presses the Back button, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system does not retain the activity's state.
4. Activities can be instantiated multiple times, even from other tasks.
Default behavior for activities and tasks:
Getting Active Through Activities Activity Stack [3]
<activity> attributes:
taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch
Intent flags:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
Controlling Activity Stack:
Getting Active Through Activities Questions...
The art and science of asking questions is the source of all knowledge.
-Thomas Berger