Chapter5 Android Activity Lifecycle and Intents
Chapter5 Android Activity Lifecycle and Intents
onStart()
onResume()
onPause()
onStop()
onRestart()
onDestroy()
Understanding Android
Lifecycle
Many disruptive things could happen while app is running
Incoming call or text message, user switches to another app, etc
http://developer.android.com/training/basics/activity-lifecycle/starting.html
OnCreate( )
Initializes activity once created
Operations typically performed in onCreate() method:
Inflate widgets and place them on screen
(e.g. using layout files with setContentView( ) )
Getting references to inflated widgets ( using
findViewbyId( ) )
Setting widget listeners to handle user interaction
Example
Paused
Running
Activity State Diagram: onPause( ) Method
Typical actions taken in onPause( ) method
Stop animations or CPU intensive tasks
Paused
Running
Activity State Diagram: Resuming Paused
App
A paused app resumes running if it becomes fully
visible and in foreground
E.g. pop-up dialog box blocking it goes away
App’s onResume( ) method is called during
transition from paused to running state
Restart videos, animations, GPS checking, etc
Paused
Running
Activity State Diagram: Stopped App
An app is stopped if it no longer visible and no
longer in foreground
E.g. user starts using another app
App’s onStop( ) method is called during transition
from paused to stopped state
Running
onStop() Method
An activity is stopped when:
User receives phone call
Running
Activity State Diagram: Starting New
App
To start new app, app is
launched
App’s onCreate( ), onStart( ) and
onResume( ) methods are called
Afterwards new app is running
Logging Errors in
Android
Logging Errors in
Android
Android can log and display various types of
errors/warnings
Error logging is in Log class of android.util package
import android.util.Log;
Turn on logging of different message types by calling
appropriate method
Logged errors/warnings displayed in Android Studio window
E.g.
Print debug
messages from each
method
Correct
Answer
Screen 2 Java
code in
CheatActivity.java
Layout
uses activity_cheat.xml
Design Layout for Screen 2
Activity 2
Write XML Layout Code for Screen 2
Activity 2
Declare New Activity in
AndroidManifest.xml
Create new activity (CheatActivity) in Android
Studio
Activity 1
Activity 2 (CheatActivity)
Activity 2 (CheatActivity)
Starting Activity 2 from Activity 1
Activity 1 starts activity 2
through the Android OS
by calling startActivity(Intent)
Passes Intent (object for communicating with Android
OS)
Build Intent
Use Intent to Start new
Activity
Parent
New Activity 2
Activity
Implicit vs Explicit
Intents
Previous example is called an explicit intent
Activity 1 and activity 2 are in same app
If Activity 2 were in another app, an implicit intent
would have to be created instead
Can also pass data between Activities 1 and 2
E.g. Activity 1 can tell new activity correct answer (True/False)
Passing Data Between
Activities
Need to pass answer (True/False from QuizActivity to
CheatActivity)
Intent
Passing Answer (True/False) as Intent
Extra
Activity receiving the Intent retrieves it using
getBooleanExtra( )
Calls
getIntent( )
Intent
Data type
References
Android Nerd Ranch, 1st edition
Busy Coder’s guide to AŶdroid versioŶ 4.4
CS 65/165 slides, Dartmouth College, Spring
2014
CS 371M slides, U of Texas Austin, Spring 2014