MobiCom on Android: Activity Lifecycle & Activity State Persistence


Published on

MobiCom on Android: Activity Lifecycle & Activity State Persistence

Published in: Software
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

MobiCom on Android: Activity Lifecycle & Activity State Persistence

  1. 1. MobiCom on Android Activity Life Cycle & Activity State Persistence Vladimir Kulyukin
  2. 2. Outline ● Review of Actvity Lifecycle ● Persisting Activity States – onSaveInstanceState() – onRestoreInstanceState()
  3. 3. Review of Activity Lifecycle
  4. 4. Review: Activitys Lifecycle ● An actvitys lifecycle is a set of states ● When the current state of an activity changes, the Android OS notifies the activity of that change ● The Android developer manages the activitys lifecycle by implementing the standard callback methods that are called on the activity object when its state changes (e.g., activity is created, stopped, resumed, and destroyed) ● Callback implementation is the only leverage the developer has over activities
  5. 5. Review: Lifecycle Callback Methods public class MyActivity { // The activity is created public void onCreate(Bundle savedInstanceState) { } // The activity is about to become visible protected void onStart() { } //The activity is visible (it is resumed) protected void onResume() { } // Another activity is taking focus (it is paused) protected void onPause() { } // The activity is no longer visible (it is stopped) protected void onStop() { } // The activity is about to be destroyed protected void onDestroy() { } }
  6. 6. Image Source:
  7. 7. Review: Activitys Lifetimes
  8. 8. Persisting Activity State
  9. 9. Managing Activity State ● When an Activity is stopped or paused, its state is preserved ● When an Activity is destroyed by the system, the next time Activity starts, it must be re- created ● The problem is that the user/developer is often unaware the the activity has been destroyed and must be recreated, which results in unpleasant surprises and crashes
  10. 10. onSaveInstanceState() ● The Android documentation states that “the system calls onSaveInstanceState() before making the activity vulnerable to destruction” ● The method onSaveInstanceState() receives a Bundle where the developer can place key/value pairs using methods putInt(), putString(), etc. ● If the system kills the application, the saved Bundle is passed to both onCreate() and onRestoreInstanceState()
  11. 11. Retriving Data from Saved Bundles ● Saved key/value pairs can be extracted from saved Bundles in onCreate() or onRestoreInstanceState() ● If nothing was saved, the passed Bundle is null ● What is the big deal? The big deal is that onSaveInstanceState() is not guaranteed to be called ● The big deal also is that there appears to be no guarantee that onRestoreInstaceState() will be called either
  12. 12. Online Doc Note on onSaveInstanceState() There is no guarantee that onSaveInstanceState() will be called before your activity is destroyed, because there are cases in which it wont be necessary to save the state (such as when the user leaves your activity using the Back button, because the user is explicitly closing the activity). If the system calls onSaveInstanceState(), it does so before onStop() and [sic] possibly before onPause(). Source:
  13. 13. Online Doc Note on onSaveInstanceState() Because onSaveInstanceState() is not guaranteed to be called you should use it to record the transient state of the activity (the state of the UI) – you should never use it to store persistent data. Instead, you should use onPause() to store persistent data when the user leaves the activity. Source:
  14. 14. onCreate() & onRestoreInstanceState() Source:
  15. 15. Activity State Persistence Experiments
  16. 16. Sample Application: Tsaritsyno Park Picture Browser Tsaritsyno Park, Moscow, Russia image source:
  17. 17. Demo: Activity Screen Flow Start Screen Up & Down Buttons Context Menu source code is here
  18. 18. Internationalization: Strings.xml <string name="title_activity_picture_browser">Tsaritsyno Image Browser</string> <string name="context_menu_title">Context Menu Options</string> <string name="context_menu_finish_item">Finish Activity</string> <string name="context_menu_grayscale_item">Grayscale Image</string> <string name="context_menu_wiki_en_item">Tsaritsyno Wikipedia</string> <string name="context_menu_wiki_ru_item">Царицыно Википедия</string> <string name="wiki_ru_url"> %D0%A6%D0%B0%D1%80%D0%B8%D1%86%D1%8B%D0%BD%D0%BE_ %28%D0%B4%D0%B2%D0%BE%D1%80%D1%86%D0%BE%D0%B2%D0%BE-%D0%BF %D0%B0%D1%80%D0%BA%D0%BE%D0%B2%D1%8B%D0%B9_%D0%B0%D0%BD %D1%81%D0%B0%D0%BC%D0%B1%D0%BB%D1%8C%29</string> <string name="wiki_en_url"></string> <string name="img_id_key">img_id</string> <string name="title_activity_picture_grayscaler">Tsaritsyno Image Grayscaler</string>
  19. 19. Internationalization: Strings.xml public boolean onContextItemSelected(MenuItem item) { switch ( item.getItemId() ) { case CONTEXT_MENU_WIKI_EN_ITEM: Intent wiki_en = new Intent(Intent.ACTION_VIEW); wiki_en.setData(Uri.parse(mRes.getString(R.string.wiki_en_url))); startActivity(wiki_en); return true; case CONTEXT_MENU_WIKI_RU_ITEM: Intent wiki_ru = new Intent(Intent.ACTION_VIEW); wiki_ru.setData(Uri.parse(mRes.getString(R.string.wiki_ru_url))); startActivity(wiki_ru); return true; case CONTEXT_MENU_GRAYSCALE_ITEM: saveSharedPrefs(); Intent grayscale = new Intent(this.getApplicationContext(), PictureProcessorAct.class); startActivity(grayscale); return true; case CONTEXT_MENU_FINISH_ITEM: this.finish(); return true; default: return false; }}
  20. 20. Experiment 01: Question Which methods are called when the PictureBrowser application is started?
  21. 21. Experiment 01: Answer onCreate(), onStart(), onResume() are called. Nothing unexpected.
  22. 22. Experiment 02: Question Suppose another application is started when PictureBrowserAct is on top of the activity stack. Which methods are called?
  23. 23. Experiment 02: Answer onSaveInstanceState(), onPause(), onPause() are called.
  24. 24. Experiment 03: Question Suppose the user presses the Back button to return to the PictureBrowser application, which methods of PictureBrowserAct will be called?
  25. 25. Experiment 03: Answer onRestart(), onStart(), onResume() are called.
  26. 26. Experiment 04: Question Suppose the user navigates to some image up or down and clicks Finish in the ContextMenu registered for the ImageView of PictureBrowserAct, will PictureBrowserAct.onSaveInstanceState() be called?
  27. 27. Experiment 04: Answer No, it is not called. The next time the user starts PictureBrowser application, image number 0 is displayed.
  28. 28. Experiment 05: Question Suppose the saving and loading of shared preferences is enabled in onCreate() and onPause(). Suppose the user navigates to some image up or down and clicks Finish in the ContextMenu registered for the ImageView of PictureBrowserAct, will the image be displayed on re-start if the image number is persisted via SharedPreferences?
  29. 29. Experiment 05: Answer Yes, it will be displayed called, because it was persisted via shared preferences.
  30. 30. References ● ●