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

554
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
554
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Mobile Application Development Lecture 13 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Shared Preferences & Inter-Component Communication ● Review of Actvity Lifecycle ● Saving Activity Stateshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 3. Shared Preferences & Inter-Component Communicationhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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 terminatedhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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()http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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); } }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 7. SharedPreferences via PreferenceManager // Use PreferenceManager and the applications context to initialize // a SharedPreferences object inside a component SharedPreferences mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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 filehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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.http://www.youtube.com/vkedco
  • 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 activitieshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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() { } }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 13. Image Source: http://developer.android.com/guide/components/activities.html#Lifecyclehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 14. Review: Activitys Lifetimeshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 15. Persisting Activity Statehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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 crasheshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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()http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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 eitherhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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: http://developer.android.com/guide/components/activities.html#Lifecyclehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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: http://developer.android.com/guide/components/activities.html#Lifecyclehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 21. onCreate() & onRestoreInstanceState() Source: http://developer.android.com/guide/components/activities.html#Lifecyclehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 22. State Persistence Experimentshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 23. Experiment 01 Q: Which methods are called when the PictureBrowser app is started? A: PictureBrowserActs onCreate(), onStart(), onResume() are called. Nothing unexpected.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 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.http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 27. References ● http://developer.android.com/reference/android/content/SharedPreferences.html ● http://developer.android.com/guide/components/activities.html#Lifecycle ● Source code available at http://www.vkedco.blogspot.com/2012/10/sharedpreferences-inter-component.htmlhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  • 28. Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com