MobiCom on Android: Activity Lifecycle & Activity State Persistence

1,158 views
1,086 views

Published on

MobiCom on Android: Activity Lifecycle & Activity State Persistence

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,158
On SlideShare
0
From Embeds
0
Number of Embeds
474
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
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 Kulyukinhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Review of Actvity Lifecycle ● Persisting Activity States – onSaveInstanceState() – onRestoreInstanceState()http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  3. 3. Review of Activity Lifecyclehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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 activitieshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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() { } }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  6. 6. Image Source: http://developer.android.com/guide/components/activities.html#Lifecyclehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  7. 7. Review: Activitys Lifetimeshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  8. 8. Persisting Activity Statehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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 crasheshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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()http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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 eitherhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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: http://developer.android.com/guide/components/activities.html#Lifecyclehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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: http://developer.android.com/guide/components/activities.html#Lifecyclehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  14. 14. onCreate() & onRestoreInstanceState() Source: http://developer.android.com/guide/components/activities.html#Lifecyclehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  15. 15. Activity State Persistence Experimentshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  16. 16. Sample Application: Tsaritsyno Park Picture Browser Tsaritsyno Park, Moscow, Russia image source: http://en.wikipedia.org/wiki/Tsaritsyno_Parkhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  17. 17. Demo: Activity Screen Flow Start Screen Up & Down Buttons Context Menu source code is herehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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">http://ru.wikipedia.org/wiki/ %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">http://en.wikipedia.org/wiki/Tsaritsyno_Park</string> <string name="img_id_key">img_id</string> <string name="title_activity_picture_grayscaler">Tsaritsyno Image Grayscaler</string>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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; }}http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  20. 20. Experiment 01: Question Which methods are called when the PictureBrowser application is started?http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  21. 21. Experiment 01: Answer onCreate(), onStart(), onResume() are called. Nothing unexpected.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  22. 22. Experiment 02: Question Suppose another application is started when PictureBrowserAct is on top of the activity stack. Which methods are called?http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  23. 23. Experiment 02: Answer onSaveInstanceState(), onPause(), onPause() are called.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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?http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  25. 25. Experiment 03: Answer onRestart(), onStart(), onResume() are called.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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?http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  27. 27. Experiment 04: Answer No, it is not called. The next time the user starts PictureBrowser application, image number 0 is displayed.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  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?http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  29. 29. Experiment 05: Answer Yes, it will be displayed called, because it was persisted via shared preferences.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  30. 30. References ● http://developer.android.com/reference/android/content/SharedPreferences.html ● http://developer.android.com/guide/components/activities.html#Lifecyclehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com

×