MobAppDev (Fall 2013): SharedPreferences, Persitence of Activity States, 2D Animations

Post on 11-May-2015

432 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

Transcript

MobAppDev

SharedPreferences, Persisting Activity States, 2D Animations

Vladimir Kulyukin

www.vkedco.blogspot.com

Outline● Shared Preferences & Inter-Component

Communication● Persistence of Activity States● 2D Animation Lists

Outline● Shared Preferences & Inter-Component

Communication● Persiting Activity States● 2D Animation Lists

Shared Preferences & Inter-Component Communication

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

Access to SharedPreferences Object

● There are three ways one can get a SharedPreferences object for a given application:

Context.getSharedPreferences() Activity.getPreferences() PreferenceManager.getDefaultSharedPreferences()

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); }}

SharedPreferences via PreferenceManager

// Use PreferenceManager and the application's context to initialize

// a SharedPreferences object inside a component

SharedPreferences mSharedPrefs =

PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

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

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();

}

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

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

Review: Lifecycle Callback Methodspublic 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() { }}

Image Source: http://developer.android.com/guide/components/activities.html#Lifecycle

Review: Activity's Lifetimes

Persistence of Activity States

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

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()

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

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: http://developer.android.com/guide/components/activities.html#Lifecycle

Android Documentation Side Note 02Because 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#Lifecycle

onCreate() & onRestoreInstanceState()

Source: http://developer.android.com/guide/components/activities.html#Lifecycle

State Persistence Experiments

Experiment 01

Q: Which methods are called when the PictureBrowser app is started?

A: PictureBrowserAct's onCreate(), onStart(), onResume() are called. Nothing unexpected.

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.

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.

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.

2D Animations

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

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

Sample Screenshots & Images

Start Screenshot End of Animation Sample Images in Image Sequences

XML Animation Lists

<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android="http://schemas.android.com/apk/res/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>

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(R.id.img_view_frame);

mImgViewFrameAnimator.setBackgroundDrawable(null);

}

// rest of the code

}

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();

}

top related