SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Android Life Cycle
    Wei-Tsung Lin
Introduction
● Android is a mobile operating system.
● There are many interrupts during normal
  using, such as a call.
● Also, most desktop users have been familiar
  with multi-task operating system, expecting
  that they can browsing the web while
  listening to the musics.
● However, the memory is not unlimited in
  mobile devices.
● So, Android introduced "Life Cycle".
Activity
● In Android, each application is a process,
  called activity.
● Android (aka Dalvik VM) maintains an
  unique activity stack to observer the status
  of process, and manager the memory.
● Life cycle of an activity is managed by
  Android OS, not by application itself.
Status of an activity
●   Active
●   Paused
●   Stopped
●   Dead
Active
● "Active" is the status that an activity is
  running.
● There will be only one "Active" activity. The
  others are int the status of "Pause",
  "Stopped", and "Dead".
Paused
● "Paused" means an activity is in the
  background.
● Creating toast, dialog, or receiving a phone
  call will make an running activity paused.
● User cannot interact with an activity which is
  paused.
Stopped
● The activity has already exit the screen, no
  other action is running.
● We can wake an stopped activity up by
  using "Notification" or multi-task button.
Dead
● The activity has been finished manually, or
  garbage collected by system.
Low memory
● When memory is running out, Dalvik will
  follow the recycle rule to release memory.

1.   Independent Activity/Service/Intent Receiver
2.   Stopped Activity (by LRU)
3.   Service
4.   Visiable Activity
5.   Running Activity
Activity API
●   OnCreate()
●   OnStart()
●   OnResume()
●   OnPause()
●   OnStop()
●   OnRestart()
●   OnDestroy()
ANR
● Android will prompt you a notification that
  "Application Not Responding" when an
  activity executing without response for a long
  time.
● Default timeout is 5 seconds.
● To avoid that, developers shouldn't execute
  a long time task in the main thread, such as
  accessing Internet, database, or playing
  music.
AsyncTask
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground (URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader .downloadFile (urls[i]);
             publishProgress ((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled ()) break;
         }
         return totalSize ;
     }

     protected void onProgressUpdate (Integer... progress) {
         setProgressPercent (progress[0]);
     }

     protected void onPostExecute (Long result) {
         showDialog ("Downloaded " + result + " bytes");
     }
 }
Service
● Four components in Android: Activity,
  Service, BroadcastReceiver,
  ContentProvider
● Except ContentProvider, the others main
  objects are running in the main thread.
● The life cycle of Service is most persistent
  (longest).
● System will kill a paused activity in very high
  possibility.
Service
● Putting a long time task into a thread is not
  enough.
● Consider rotating your device. Actually the
  activity is killed, and then system recreate a
  new activity rotated. The thread will be killed
  too!
● You should not only put the long time task
  into a new thread, but also a Service.
● Although the original activity is killed, the
  service will execute as usual. You can make
  inter-process communication later.
MessageQueue
● Android implement a message queue to
  handle messages, which called Looper.
● It's very helpful when you want to update UI
  outside the main thread(UI thread).
● A handler can push a message into a looper,
  dispatch, and handler the message in the
  queue.
● When creating a handler, it will bind to the
  looper of current activity automatically. Also,
  you can assign a specific looper to bind.
IntentService
● IntentService is actually a Service.
● Using IntentService, you don't need to
  implement Looper, Message, and Handler
  yourself.
  Intent intent = new Intent(MainActivity.this, MyIntentService.class);
  intent.putExtra("tag", "message");
  startService(intent);


  in MyIntentService:

  @Override
  protected void onHandleIntent(Intent intent) {
     String message = intent.getStringExtra("tag");
  }
Reference
http://developer.android.
com/training/basics/activity-lifecycle/index.html
http://developer.android.
com/guide/components/services.html
http://developer.android.
com/reference/android/os/Handler.html
http://developer.android.
com/reference/android/os/Looper.html
http://developer.android.
com/reference/android/app/IntentService.html

Contenu connexe

Tendances (20)

Android notification
Android notificationAndroid notification
Android notification
 
android activity
android activityandroid activity
android activity
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
 
Notification android
Notification androidNotification android
Notification android
 
Android animation
Android animationAndroid animation
Android animation
 
Location-Based Services on Android
Location-Based Services on AndroidLocation-Based Services on Android
Location-Based Services on Android
 
Introduction to Android ppt
Introduction to Android pptIntroduction to Android ppt
Introduction to Android ppt
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Android activity
Android activityAndroid activity
Android activity
 
Android Services
Android ServicesAndroid Services
Android Services
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Android Navigation Component
Android Navigation ComponentAndroid Navigation Component
Android Navigation Component
 
Android ppt
Android pptAndroid ppt
Android ppt
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Android - Android Intent Types
Android - Android Intent TypesAndroid - Android Intent Types
Android - Android Intent Types
 
Android ppt
Android ppt Android ppt
Android ppt
 
Job schedulers android
Job schedulers androidJob schedulers android
Job schedulers android
 
Action Bar in Android
Action Bar in AndroidAction Bar in Android
Action Bar in Android
 
Android Fragment
Android FragmentAndroid Fragment
Android Fragment
 
android layouts
android layoutsandroid layouts
android layouts
 

En vedette

Android Life Cycle
Android Life CycleAndroid Life Cycle
Android Life Cyclemssaman
 
The Implementation of a Waiting List Management and eScheduling System to Imp...
The Implementation of a Waiting List Management and eScheduling System to Imp...The Implementation of a Waiting List Management and eScheduling System to Imp...
The Implementation of a Waiting List Management and eScheduling System to Imp...Health Informatics New Zealand
 
android architecture,life cycle,sdk,execution process
android architecture,life cycle,sdk,execution processandroid architecture,life cycle,sdk,execution process
android architecture,life cycle,sdk,execution processDeepa Rani
 
AAL Forum - OHA: Open Health Assistant Care Management & Services Integratio...
AAL Forum - OHA: Open Health Assistant Care Management  & Services Integratio...AAL Forum - OHA: Open Health Assistant Care Management  & Services Integratio...
AAL Forum - OHA: Open Health Assistant Care Management & Services Integratio...Ándago
 
Android session 2-behestee
Android session 2-behesteeAndroid session 2-behestee
Android session 2-behesteeHussain Behestee
 
Dalvik Source Code Reading
Dalvik Source Code ReadingDalvik Source Code Reading
Dalvik Source Code Readingkishima7
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHPNisa Soomro
 
Android custom listview
Android custom listviewAndroid custom listview
Android custom listviewparmistech
 
Post-PC: Geolocation & Maps in the Android Ecosystem
Post-PC: Geolocation & Maps in the Android EcosystemPost-PC: Geolocation & Maps in the Android Ecosystem
Post-PC: Geolocation & Maps in the Android EcosystemMichael Genkin
 
Persistence in Android
Persistence in AndroidPersistence in Android
Persistence in Androidma-polimi
 
Manipulating Android tasks and back stack
Manipulating Android tasks and back stackManipulating Android tasks and back stack
Manipulating Android tasks and back stackRan Nachmany
 
Painless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldPainless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldChristian Melchior
 
android content providers
android content providersandroid content providers
android content providersDeepa Rani
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Androidma-polimi
 
Inter-process communication of Android
Inter-process communication of AndroidInter-process communication of Android
Inter-process communication of AndroidTetsuyuki Kobayashi
 

En vedette (20)

Google android Activity lifecycle
Google android Activity lifecycle Google android Activity lifecycle
Google android Activity lifecycle
 
Android Life Cycle
Android Life CycleAndroid Life Cycle
Android Life Cycle
 
The Implementation of a Waiting List Management and eScheduling System to Imp...
The Implementation of a Waiting List Management and eScheduling System to Imp...The Implementation of a Waiting List Management and eScheduling System to Imp...
The Implementation of a Waiting List Management and eScheduling System to Imp...
 
Application lifecycle
Application lifecycleApplication lifecycle
Application lifecycle
 
android architecture,life cycle,sdk,execution process
android architecture,life cycle,sdk,execution processandroid architecture,life cycle,sdk,execution process
android architecture,life cycle,sdk,execution process
 
AAL Forum - OHA: Open Health Assistant Care Management & Services Integratio...
AAL Forum - OHA: Open Health Assistant Care Management  & Services Integratio...AAL Forum - OHA: Open Health Assistant Care Management  & Services Integratio...
AAL Forum - OHA: Open Health Assistant Care Management & Services Integratio...
 
Android session 2-behestee
Android session 2-behesteeAndroid session 2-behestee
Android session 2-behestee
 
Dalvik Source Code Reading
Dalvik Source Code ReadingDalvik Source Code Reading
Dalvik Source Code Reading
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
 
Android custom listview
Android custom listviewAndroid custom listview
Android custom listview
 
Android Data Persistence
Android Data PersistenceAndroid Data Persistence
Android Data Persistence
 
Post-PC: Geolocation & Maps in the Android Ecosystem
Post-PC: Geolocation & Maps in the Android EcosystemPost-PC: Geolocation & Maps in the Android Ecosystem
Post-PC: Geolocation & Maps in the Android Ecosystem
 
Logcatの話
Logcatの話Logcatの話
Logcatの話
 
Persistence in Android
Persistence in AndroidPersistence in Android
Persistence in Android
 
Android Custom view
Android Custom view Android Custom view
Android Custom view
 
Manipulating Android tasks and back stack
Manipulating Android tasks and back stackManipulating Android tasks and back stack
Manipulating Android tasks and back stack
 
Painless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldPainless Persistence in a Disconnected World
Painless Persistence in a Disconnected World
 
android content providers
android content providersandroid content providers
android content providers
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Android
 
Inter-process communication of Android
Inter-process communication of AndroidInter-process communication of Android
Inter-process communication of Android
 

Similaire à Android life cycle

Dori waldman android _course
Dori waldman android _courseDori waldman android _course
Dori waldman android _courseDori Waldman
 
Dori waldman android _course_2
Dori waldman android _course_2Dori waldman android _course_2
Dori waldman android _course_2Dori Waldman
 
Anatomy of android application
Anatomy of android applicationAnatomy of android application
Anatomy of android applicationNikunj Dhameliya
 
Lecture #4 activities &amp; fragments
Lecture #4  activities &amp; fragmentsLecture #4  activities &amp; fragments
Lecture #4 activities &amp; fragmentsVitali Pekelis
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart JfokusLars Vogel
 
Inside the Android application framework - Google I/O 2009
Inside the Android application framework - Google I/O 2009Inside the Android application framework - Google I/O 2009
Inside the Android application framework - Google I/O 2009Viswanath J
 
Five android architecture
Five android architectureFive android architecture
Five android architectureTomislav Homan
 
Not Quite As Painful Threading
Not Quite As Painful ThreadingNot Quite As Painful Threading
Not Quite As Painful ThreadingCommonsWare
 
Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012Tomáš Kypta
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Gabor Varadi
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8Utkarsh Mankad
 
Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Opersys inc.
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycleAhsanul Karim
 
2013-05-15 threads. why and how
2013-05-15 threads. why and how2013-05-15 threads. why and how
2013-05-15 threads. why and howCocoaHeads Tricity
 
Android Connecting to internet Part 2
Android  Connecting to internet Part 2Android  Connecting to internet Part 2
Android Connecting to internet Part 2Paramvir Singh
 
Lecture #3 activities and intents
Lecture #3  activities and intentsLecture #3  activities and intents
Lecture #3 activities and intentsVitali Pekelis
 

Similaire à Android life cycle (20)

Dori waldman android _course
Dori waldman android _courseDori waldman android _course
Dori waldman android _course
 
Dori waldman android _course_2
Dori waldman android _course_2Dori waldman android _course_2
Dori waldman android _course_2
 
Android
AndroidAndroid
Android
 
Anatomy of android application
Anatomy of android applicationAnatomy of android application
Anatomy of android application
 
Lecture #4 activities &amp; fragments
Lecture #4  activities &amp; fragmentsLecture #4  activities &amp; fragments
Lecture #4 activities &amp; fragments
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart Jfokus
 
Inside the Android application framework - Google I/O 2009
Inside the Android application framework - Google I/O 2009Inside the Android application framework - Google I/O 2009
Inside the Android application framework - Google I/O 2009
 
Five android architecture
Five android architectureFive android architecture
Five android architecture
 
fragments-activity.pptx
fragments-activity.pptxfragments-activity.pptx
fragments-activity.pptx
 
Not Quite As Painful Threading
Not Quite As Painful ThreadingNot Quite As Painful Threading
Not Quite As Painful Threading
 
Android101
Android101Android101
Android101
 
Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)
 
Unit I- ANDROID OVERVIEW.ppt
Unit I- ANDROID OVERVIEW.pptUnit I- ANDROID OVERVIEW.ppt
Unit I- ANDROID OVERVIEW.ppt
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8
 
Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
 
2013-05-15 threads. why and how
2013-05-15 threads. why and how2013-05-15 threads. why and how
2013-05-15 threads. why and how
 
Android Connecting to internet Part 2
Android  Connecting to internet Part 2Android  Connecting to internet Part 2
Android Connecting to internet Part 2
 
Lecture #3 activities and intents
Lecture #3  activities and intentsLecture #3  activities and intents
Lecture #3 activities and intents
 

Android life cycle

  • 1. Android Life Cycle Wei-Tsung Lin
  • 2. Introduction ● Android is a mobile operating system. ● There are many interrupts during normal using, such as a call. ● Also, most desktop users have been familiar with multi-task operating system, expecting that they can browsing the web while listening to the musics. ● However, the memory is not unlimited in mobile devices. ● So, Android introduced "Life Cycle".
  • 3. Activity ● In Android, each application is a process, called activity. ● Android (aka Dalvik VM) maintains an unique activity stack to observer the status of process, and manager the memory. ● Life cycle of an activity is managed by Android OS, not by application itself.
  • 4. Status of an activity ● Active ● Paused ● Stopped ● Dead
  • 5. Active ● "Active" is the status that an activity is running. ● There will be only one "Active" activity. The others are int the status of "Pause", "Stopped", and "Dead".
  • 6. Paused ● "Paused" means an activity is in the background. ● Creating toast, dialog, or receiving a phone call will make an running activity paused. ● User cannot interact with an activity which is paused.
  • 7. Stopped ● The activity has already exit the screen, no other action is running. ● We can wake an stopped activity up by using "Notification" or multi-task button.
  • 8. Dead ● The activity has been finished manually, or garbage collected by system.
  • 9. Low memory ● When memory is running out, Dalvik will follow the recycle rule to release memory. 1. Independent Activity/Service/Intent Receiver 2. Stopped Activity (by LRU) 3. Service 4. Visiable Activity 5. Running Activity
  • 10. Activity API ● OnCreate() ● OnStart() ● OnResume() ● OnPause() ● OnStop() ● OnRestart() ● OnDestroy()
  • 11. ANR ● Android will prompt you a notification that "Application Not Responding" when an activity executing without response for a long time. ● Default timeout is 5 seconds. ● To avoid that, developers shouldn't execute a long time task in the main thread, such as accessing Internet, database, or playing music.
  • 12. AsyncTask private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground (URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader .downloadFile (urls[i]); publishProgress ((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled ()) break; } return totalSize ; } protected void onProgressUpdate (Integer... progress) { setProgressPercent (progress[0]); } protected void onPostExecute (Long result) { showDialog ("Downloaded " + result + " bytes"); } }
  • 13. Service ● Four components in Android: Activity, Service, BroadcastReceiver, ContentProvider ● Except ContentProvider, the others main objects are running in the main thread. ● The life cycle of Service is most persistent (longest). ● System will kill a paused activity in very high possibility.
  • 14. Service ● Putting a long time task into a thread is not enough. ● Consider rotating your device. Actually the activity is killed, and then system recreate a new activity rotated. The thread will be killed too! ● You should not only put the long time task into a new thread, but also a Service. ● Although the original activity is killed, the service will execute as usual. You can make inter-process communication later.
  • 15. MessageQueue ● Android implement a message queue to handle messages, which called Looper. ● It's very helpful when you want to update UI outside the main thread(UI thread). ● A handler can push a message into a looper, dispatch, and handler the message in the queue. ● When creating a handler, it will bind to the looper of current activity automatically. Also, you can assign a specific looper to bind.
  • 16. IntentService ● IntentService is actually a Service. ● Using IntentService, you don't need to implement Looper, Message, and Handler yourself. Intent intent = new Intent(MainActivity.this, MyIntentService.class); intent.putExtra("tag", "message"); startService(intent); in MyIntentService: @Override protected void onHandleIntent(Intent intent) { String message = intent.getStringExtra("tag"); }