Your SlideShare is downloading. ×
Mobile Application Development: Lecture 13
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Mobile Application Development: Lecture 13


Published on

Published in: Technology

  • 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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Mobile Application Development Lecture 13 Vladimir Kulyukin Department of Computer Science Utah State University
  • 2. Outline ● Shared Preferences & Inter-Component Communication ● Review of Actvity Lifecycle ● Saving Activity States
  • 3. Shared Preferences & Inter-Component Communication
  • 4. Shared Preferences ● SharedPreferences class provides a framework for saving and retrieving key/value pairs of primitive data ● Commonly used for saving users application preferences, UI states, application settings ● The saved data persist across user sessions even when the application is terminated
  • 5. Access to SharedPreferences Object ● There are three ways one can get a SharedPreferences object for a given application: – Context.getSharedPreferences() – Activity.getPreferences() – PreferenceManager.getDefaultSharedPreferences()
  • 6. SharedPreferences via Context public class MyAct extends Activity { // The activity is created public void onCreate(Bundle savedInstanceState) { static final String PREFS_FILE = “MySharedPrefs”; // some code SharedPreferences sprefs = getSharedPreferences(PREFS_FILE, MODE_PRIVATE); int my value = sprefs.getInt(“my key”, -1); } }
  • 7. SharedPreferences via PreferenceManager // Use PreferenceManager and the applications context to initialize // a SharedPreferences object inside a component SharedPreferences mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  • 8. Which Access Method to Use? ● Contex.getSharedPreferences() should be used if you have to identify multiple preferences files by name ● Activity.getPreferences() should be used when you have only one preference file for your activity ● PreferenceManager.getDefaultSharedPreferen ces() should be used when multiple components within the same application share the same default preference file
  • 9. Persisting Shared Preferences // grab the strings entered by the user, put them into the SharedPreferences // Editor under appropirate keys and persist them via commit(). private void saveSharedPrefs() { SharedPreferences.Editor spEditor = mSharedPreferences.edit(); spEditor.putInt(“my key”, 10); spEditor.commit(); }
  • 10. Example 01 Implement an application, PictureBrowser with two activities, PictureBrowserAct and PictureProcessAct. PictureBrowserAct allows the user to browse up and down through a collection of images and grayscale each image via a ContextMenu. PictureProcessorAct displays the grayscale image. The two activities communicate via shared preferences.
  • 11. 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
  • 12. 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() { } }
  • 13. Image Source:
  • 14. Review: Activitys Lifetimes
  • 15. Persisting Activity State
  • 16. 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
  • 17. 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()
  • 18. 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
  • 19. Android Documentation Side Note 01 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:
  • 20. Android Documentation Side Note 02 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:
  • 21. onCreate() & onRestoreInstanceState() Source:
  • 22. State Persistence Experiments
  • 23. Experiment 01 Q: Which methods are called when the PictureBrowser app is started? A: PictureBrowserActs onCreate(), onStart(), onResume() are called. Nothing unexpected.
  • 24. Experiment 02 Q: Suppose that FamousMathematiciansBrowser app is started when PictureBrowserAct is on top of the activity stack. 1) Which methods are called? 2) Which methods are called after the user presses Back from FamousMathematiciansBrowserAct? A: 1) PictureBrowserActs onSaveInstanceState(), onPause() are called; 2) PictureBrowserActs onRestart(), onStart(), onResume(). OnRestoreInstanceState() is not called.
  • 25. Experiment 03 Q: Suppose the user navigates to image 7 and clicks Finish in the ContextMenu registered for the ImageView of PictureBrowserAct, will PictureBrowserAct.onSaveInstanceState() be called? A: No, it is not called. The next time the user starts PictureBrowser application, image number 0 is displayed.
  • 26. Experiment 04 Q: Suppose we persist data in onPause() via SharedPreferences and load it in onCreate(), will the image the user navigated to before PictureBrowserAct is finished be displayed in the restarted PictureBrowserAct? A: Yes, it will be.
  • 27. References ● ● ● Source code available at
  • 28. Feedback Bugs to vladimir dot kulyukin at gmail dot com