Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lecture 3 getting active through activities


Published on

Published in: Technology, Education
  • Be the first to comment

Lecture 3 getting active through activities

  1. 1. Android Application Development Lecture 3: Getting Active Through Activities and Intents 1. Android App Anatomy 2. What is Activity 3. Activity Lifecycle 4. Handling Runtime Changes 5. Multiple Activities 7. Activity Stack 8. Intents Ahsanul Karim
  2. 2. Getting Active Through Activities Today We'll Discuss... 1. Android App Anatomy 4. Handling Runtime Changes 2. What is Activity 5. Multiple Activities 3. Activity Lifecycle 6. Activity Stack
  3. 3. Getting Active Through Activities Application Anatomy There are 4 types of application components/building blocks: Activities 1. Activity provides user interface 2. Usually represents a single screen 3. Can contain one or more views 4. Extends the Activity base class BroadcastReceiver 1. Receives and Reacts to broadcast Intents 2. No UI but can start an Activity 3. Extends the BroadcastReceiver base class Services 1. No user interface 2. Runs in background 3. Extends the Service base class ContentProvider s 1. Makes application data available to other apps [data sharing] 2. Uses SQLite database as storage 3. Extends the ContentProvider base class
  4. 4. 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).
  5. 5. Activity Lifecycle
  6. 6. 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...
  7. 7. 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.
  8. 8. 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.
  9. 9. 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()
  10. 10. Getting Active Through Activities Activity Lifecycle [6] Pausing: onPause() We should usually use the onPause() callback to: 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. Operations in onPause() must be kept simple in order to allow for a speedy transition to the user's next destination.
  11. 11. 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).
  12. 12. 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.
  13. 13. 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).
  14. 14. 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 change Allow the activity to restart when a configuration changes, but carry a stateful Object to the new instance of activity. Handle the configuration change yourself Prevent the system from restarting activity but receive a callback when the config changes. .
  15. 15. 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
  16. 16. Getting Active Through Activities 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. Activity Stack [1]
  17. 17. Getting Active Through Activities Activity Stack [2] Default behavior for activities and tasks: 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.
  18. 18. Getting Active Through Activities Activity Stack [3] Controlling Activity Stack: <activity> attributes: taskAffinity Intent flags: launchMode FLAG_ACTIVITY_NEW_TASK allowTaskReparenting FLAG_ACTIVITY_CLEAR_TOP clearTaskOnLaunch FLAG_ACTIVITY_SINGLE_TOP alwaysRetainTaskState finishOnTaskLaunch
  19. 19. Getting Active Through Activities Questions... The art and science of asking questions is the source of all knowledge. -Thomas Berger