MobAppDev: SharedPreferences, Persitence of Activity States, 2D Animations


Published on

SharedPreferences, Persitence of Activity States, 2D Animations

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

MobAppDev: SharedPreferences, Persitence of Activity States, 2D Animations

  1. 1. MobAppDev SharedPreferences, Persisting Activity States, 2D Animations Vladimir Kulyukin
  2. 2. Outline ● ● ● Shared Preferences & Inter-Component Communication Persistence of Activity States 2D Animation Lists
  3. 3. Outline ● ● ● Shared Preferences & Inter-Component Communication Persiting Activity States 2D Animation Lists
  4. 4. Shared Preferences & Inter-Component Communication
  5. 5. Shared Preferences ● ● ● SharedPreferences class provides a framework for saving and retrieving key/value pairs of primitive data Commonly used for saving user's application preferences, UI states, application settings The saved data persist across user sessions even when the application is terminated
  6. 6. Access to SharedPreferences Object ● There are three ways one can get a SharedPreferences object for a given application:  Context.getSharedPreferences()  Activity.getPreferences()  PreferenceManager.getDefaultSharedPreferences()
  7. 7. 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); } }
  8. 8. SharedPreferences via PreferenceManager // Use PreferenceManager and the application's context to initialize // a SharedPreferences object inside a component SharedPreferences mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  9. 9. 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.getDefaultSharedPreferences() should be used when multiple components within the same application share the same default preference file
  10. 10. 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(); }
  11. 11. Problem 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. source code is here
  12. 12. Review: Activity's Lifecycle ● ● ● ● An actvity's 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 activity's 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
  13. 13. 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() { } }
  14. 14. Image Source:
  15. 15. Review: Activity's Lifetimes
  16. 16. Persistence of Activity States
  17. 17. 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
  18. 18. 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()
  19. 19. 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
  20. 20. 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 won't 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:
  21. 21. 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:
  22. 22. onCreate() & onRestoreInstanceState() Source:
  23. 23. State Persistence Experiments
  24. 24. Experiment 01 Q: Which methods are called when the PictureBrowser app is started? A: PictureBrowserAct's onCreate(), onStart(), onResume() are called. Nothing unexpected.
  25. 25. Experiment 02 Q: Suppose that another 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 another application? A: 1) PictureBrowserAct's onSaveInstanceState(), onPause() are called; 2) PictureBrowserAct's onRestart(), onStart(), onResume(). OnRestoreInstanceState() is not called.
  26. 26. 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.
  27. 27. 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.
  28. 28. 2D Animations
  29. 29. Animation Lists ● ● Animation list is a simple way to do 2D animations There are several steps in developing a 2D animation with animation lists:  Create images for specific image sequences  Place the images into res/drawable  Create XML animation lists for each image sequence  Use ImageView and AnimationDrawable objects to play image sequences
  30. 30. Problem Implement an application, TicTacToe2DAnimator that uses image sequences to show the player animations for games in which X wins, in which O wins, and which end in a draw. source code is here
  31. 31. Sample Screenshots & Images Start Screenshot End of Animation Sample Images in Image Sequences
  32. 32. XML Animation Lists <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="" > <item android:drawable="@drawable/x_wins_00" android:duration="500" /> <item android:drawable="@drawable/x_wins_01" android:duration="500" /> <item android:drawable="@drawable/x_wins_02" android:duration="500" /> <item android:drawable="@drawable/x_wins_03" android:duration="500" /> <item android:drawable="@drawable/x_wins_04" android:duration="500" /> <item android:drawable="@drawable/x_wins_05" android:duration="500" /> <item android:drawable="@drawable/x_wins_06" android:duration="500" /> <item android:drawable="@drawable/x_wins_07" android:duration="500" /> <item android:drawable="@drawable/x_wins_08" android:duration="500" /> </animation-list>
  33. 33. Playing 2D Animations public class TicTacToe2DAnimatorAct extends Activity { ImageView mImgViewFrameAnimator = null; AnimationDrawable mAnimDraw = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tic_tac_toe_2d_animator_activity); mImgViewFrameAnimator = (ImageView) this.findViewById(; mImgViewFrameAnimator.setBackgroundDrawable(null); } // rest of the code }
  34. 34. Launching 2D Animations void animateTicTacToe_X_WINS() { mImgViewFrameAnimator.setBackgroundResource(R.drawable.tic_tac_toe_x_wins_animation_list); mImgViewFrameAnimator.setVisibility(ImageView.VISIBLE); mAnimDraw = (AnimationDrawable) mImgViewFrameAnimator.getBackground(); if ( mAnimDraw.isRunning() ) mAnimDraw.stop(); mAnimDraw.start(); }