SlideShare a Scribd company logo
2
Most read
4
Most read
6
Most read
The Activity lifecycle
●
Apps, memory, and storage
storage: Your device has apps and files installed
and stored on its internal disk, SD card, etc.
 Settings  Storage
● memory: Some subset of apps might be
currently loaded into the device's RAM and
are either running or ready to be run.



When the user loads an app,
it is loaded from storage into memory.
When the user exits an app, it might be
cleared from memory, or might remain
in memory so you can go back to it later.
See which apps are in memory:
●
Settings  Apps  Running
●
Activity state
An activity can be thought of as being in one of several states:





starting: In process of loading up, but not fully loaded.
running: Done loading and now visible on the screen.
paused: Partially obscured or out of focus, but not shut down.
stopped: No longer active, but still in the device's active memory.
destroyed: Shut down and no longer currently loaded in memory.
● Transitions between these states are represented by events that
you can listen to in your activity code.
 onCreate, onPause, onResume, onStop, onDestroy, ...
Activity lifecycle
Other diagrams
●
The onCreate method
In onCreate, you create and set up the activity
object, load any static resources like images,
layouts, set up menus etc.


after this, the Activity object exists
think of this as the "constructor" of the activity
public class FooActivity extends Activity {
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // always call super
setContentView(R.layout.activity_foo); // set up layout
any other initialization code; // anything else you need
}
}
●
●
The onPause method
When onPause is called, your activity is
still partially visible.
May be temporary, or on way to termination.



Stop animations or other actions that consume CPU.
Commit unsaved changes (e.g. draft email).
Release system resources that affect battery life.
// always call super
// release resources
public void onPause() {
super.onPause();
if (myConnection != null) {
myConnection.close();
myConnection = null;
}
}
●
●
The onResume method
When onResume is called, your activity is
coming out of the Paused state and
into the Running state again.
Also called when activity is first created/loaded!


Initialize resources that you will release in onPause.
Start/resume animations or other ongoing actions
that should only run when activity is visible on screen.
// always call super
public void onResume() {
super.onPause();
if (myConnection == null) {
// init.resourcesmyConnection = new ExampleConnect();
myConnection.connect();
}
}
●
The onStop method
When onStop is called, your activity
is no longer visible on the screen:



User chose another app from Recent Apps window.
User starts a different activity in your app.
User receives a phone call while in your app.
● Your app might still be running,
but that activity is not.


onPause is always called before onStop.
onStop performs heavy-duty shutdown tasks like writing to a database.
// always call super
public void onStop() {
super.onStop();
...
}
●
●
onStart and onRestart
onStart is called every time the activity begins.
onRestart is called when activity was stopped
but is started again later (all but the first start).


Not as commonly used; favor onResume.
Re-open any resources that onStop closed.
// always call super
// always call super
public void onStart() {
super.onStart();
...
}
public void onRestart() {
super.onRestart();
...
}
●
The onDestroy method
When onDestroy is called, your entire app is
being shut down and unloaded from memory.



Unpredictable exactly when/if it will be called.
Can be called whenever the system wants to
reclaim the memory used by your app.
Generally favor onPause or onStop because they
are called in a predictable and timely manner.
// always call super
public void onDestroy() {
super.onDestroy();
...
}
●
Testing activity states (link)
Use the LogCat system for logging messages when your app
changes states:


analogous to System.out.println debugging for Android apps
appears in the LogCat console in Android Studio
public void onStart() {
super.onStart();
Log.v("testing", "onStart was called!");
}
Method Description
Log.d("tag","message"); debugmessage(fordebugging)
Log.e("tag","message"); errormessage(fatalerror)
Log.i("tag","message"); infomessage(low-urgencyFYI)
Log.v("tag","message"); verbosemessage(rarelyshown)
Log.w("tag","message"); warningmessage(non-fatalerror)
Log.wtf("tag",exception); logstacktraceofanexception
Log methods
●
Each method can also accept an optional exception argument:
try { someCode(); }
catch (Exception ex) {
Log.e("error4", "something went wrong", ex);
}

More Related Content

PPTX
04 activities and activity life cycle
PPTX
Android activity lifecycle
PPT
android activity
PPTX
PDF
Android activity
PDF
Introduction to fragments in android
PDF
Android life cycle
PPTX
Android application-component
04 activities and activity life cycle
Android activity lifecycle
android activity
Android activity
Introduction to fragments in android
Android life cycle
Android application-component

What's hot (20)

PPTX
SQLite database in android
PPTX
Android share preferences
PPTX
Presentation on Android application life cycle and saved instancestate
PPTX
Deadlock Prevention
PDF
Android Fragment
PPTX
Mobile application development ppt
PPTX
Google android Activity lifecycle
PDF
Android datastorage
PPTX
System calls
PDF
Android Basic Components
PPTX
Linux file system
PDF
Deadlock
PPT
Deadlock
PPTX
Broadcast Receiver
PPTX
Dead Lock in operating system
PDF
Java Thread Synchronization
PDF
Layouts in android
PDF
Android Location and Maps
PPT
Basic concept of OOP's
PDF
operating system structure
SQLite database in android
Android share preferences
Presentation on Android application life cycle and saved instancestate
Deadlock Prevention
Android Fragment
Mobile application development ppt
Google android Activity lifecycle
Android datastorage
System calls
Android Basic Components
Linux file system
Deadlock
Deadlock
Broadcast Receiver
Dead Lock in operating system
Java Thread Synchronization
Layouts in android
Android Location and Maps
Basic concept of OOP's
operating system structure
Ad

Similar to Activity lifecycle (20)

PPTX
Lecture 6: Android Activity Life Cycle.pptx
PDF
02 programmation mobile - android - (activity, view, fragment)
DOC
Day 4: Activity lifecycle
ODP
Anatomy of android application
PDF
Android Activities.pdf
PPT
Activities
PPTX
Architecting Single Activity Applications (With or Without Fragments)
PPTX
Lesson 4
PPTX
presentation on cpu process state for btech bca.pptx
ODP
What to do after Rooting ?
PPTX
Android - Message
PPTX
Lecture #4 activities & fragments
PPTX
process state presentation by Deepak Thakur
PPTX
Process state in OS
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
PPT
Operatingsystems lecture2
PDF
process.pdfzljwiyrouyaeutoaetodtusiokklhh
PPTX
5 - ch3.pptx
PDF
Windows Phone 8 Fundamental
Lecture 6: Android Activity Life Cycle.pptx
02 programmation mobile - android - (activity, view, fragment)
Day 4: Activity lifecycle
Anatomy of android application
Android Activities.pdf
Activities
Architecting Single Activity Applications (With or Without Fragments)
Lesson 4
presentation on cpu process state for btech bca.pptx
What to do after Rooting ?
Android - Message
Lecture #4 activities & fragments
process state presentation by Deepak Thakur
Process state in OS
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
Operatingsystems lecture2
process.pdfzljwiyrouyaeutoaetodtusiokklhh
5 - ch3.pptx
Windows Phone 8 Fundamental
Ad

Recently uploaded (10)

DOC
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
PDF
Kids, Screens & Emotional Development by Meenakshi Khakat
DOC
NIU毕业证学历认证,阿比林基督大学毕业证留学生学历
PDF
Best 4 Sites for Buy Verified Cash App Accounts – BTC Only.pdf
PPTX
ASMS Telecommunication company Profile
PDF
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
PDF
2025 Guide to Buy Verified Cash App Accounts You Can Trust.pdf
DOC
SIUE毕业证学历认证,阿祖萨太平洋大学毕业证学位证书复制
PPTX
Social Media People PowerPoint Templates.pptx
PPTX
Introduction to Packet Tracer Course Overview - Aug 21 (1).pptx
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
Kids, Screens & Emotional Development by Meenakshi Khakat
NIU毕业证学历认证,阿比林基督大学毕业证留学生学历
Best 4 Sites for Buy Verified Cash App Accounts – BTC Only.pdf
ASMS Telecommunication company Profile
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
2025 Guide to Buy Verified Cash App Accounts You Can Trust.pdf
SIUE毕业证学历认证,阿祖萨太平洋大学毕业证学位证书复制
Social Media People PowerPoint Templates.pptx
Introduction to Packet Tracer Course Overview - Aug 21 (1).pptx

Activity lifecycle

  • 2. ● Apps, memory, and storage storage: Your device has apps and files installed and stored on its internal disk, SD card, etc.  Settings  Storage ● memory: Some subset of apps might be currently loaded into the device's RAM and are either running or ready to be run.    When the user loads an app, it is loaded from storage into memory. When the user exits an app, it might be cleared from memory, or might remain in memory so you can go back to it later. See which apps are in memory: ● Settings  Apps  Running
  • 3. ● Activity state An activity can be thought of as being in one of several states:      starting: In process of loading up, but not fully loaded. running: Done loading and now visible on the screen. paused: Partially obscured or out of focus, but not shut down. stopped: No longer active, but still in the device's active memory. destroyed: Shut down and no longer currently loaded in memory. ● Transitions between these states are represented by events that you can listen to in your activity code.  onCreate, onPause, onResume, onStop, onDestroy, ...
  • 6. ● The onCreate method In onCreate, you create and set up the activity object, load any static resources like images, layouts, set up menus etc.   after this, the Activity object exists think of this as the "constructor" of the activity public class FooActivity extends Activity { ... public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // always call super setContentView(R.layout.activity_foo); // set up layout any other initialization code; // anything else you need } }
  • 7. ● ● The onPause method When onPause is called, your activity is still partially visible. May be temporary, or on way to termination.    Stop animations or other actions that consume CPU. Commit unsaved changes (e.g. draft email). Release system resources that affect battery life. // always call super // release resources public void onPause() { super.onPause(); if (myConnection != null) { myConnection.close(); myConnection = null; } }
  • 8. ● ● The onResume method When onResume is called, your activity is coming out of the Paused state and into the Running state again. Also called when activity is first created/loaded!   Initialize resources that you will release in onPause. Start/resume animations or other ongoing actions that should only run when activity is visible on screen. // always call super public void onResume() { super.onPause(); if (myConnection == null) { // init.resourcesmyConnection = new ExampleConnect(); myConnection.connect(); } }
  • 9. ● The onStop method When onStop is called, your activity is no longer visible on the screen:    User chose another app from Recent Apps window. User starts a different activity in your app. User receives a phone call while in your app. ● Your app might still be running, but that activity is not.   onPause is always called before onStop. onStop performs heavy-duty shutdown tasks like writing to a database. // always call super public void onStop() { super.onStop(); ... }
  • 10. ● ● onStart and onRestart onStart is called every time the activity begins. onRestart is called when activity was stopped but is started again later (all but the first start).   Not as commonly used; favor onResume. Re-open any resources that onStop closed. // always call super // always call super public void onStart() { super.onStart(); ... } public void onRestart() { super.onRestart(); ... }
  • 11. ● The onDestroy method When onDestroy is called, your entire app is being shut down and unloaded from memory.    Unpredictable exactly when/if it will be called. Can be called whenever the system wants to reclaim the memory used by your app. Generally favor onPause or onStop because they are called in a predictable and timely manner. // always call super public void onDestroy() { super.onDestroy(); ... }
  • 12. ● Testing activity states (link) Use the LogCat system for logging messages when your app changes states:   analogous to System.out.println debugging for Android apps appears in the LogCat console in Android Studio public void onStart() { super.onStart(); Log.v("testing", "onStart was called!"); }
  • 13. Method Description Log.d("tag","message"); debugmessage(fordebugging) Log.e("tag","message"); errormessage(fatalerror) Log.i("tag","message"); infomessage(low-urgencyFYI) Log.v("tag","message"); verbosemessage(rarelyshown) Log.w("tag","message"); warningmessage(non-fatalerror) Log.wtf("tag",exception); logstacktraceofanexception Log methods ● Each method can also accept an optional exception argument: try { someCode(); } catch (Exception ex) { Log.e("error4", "something went wrong", ex); }