mobappdev (fall 2013): sharedpreferences, persitence of activity states, 2d animations

34
MobAppDev SharedPreferences, Persisting Activity States, 2D Animations Vladimir Kulyukin www.vkedco.blogspot.com

Upload: vladimir-kulyukin

Post on 11-May-2015

432 views

Category:

Technology


0 download

TRANSCRIPT

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

MobAppDev

SharedPreferences, Persisting Activity States, 2D Animations

Vladimir Kulyukin

www.vkedco.blogspot.com

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

Outline● Shared Preferences & Inter-Component

Communication● Persistence of Activity States● 2D Animation Lists

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

Outline● Shared Preferences & Inter-Component

Communication● Persiting Activity States● 2D Animation Lists

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

Shared Preferences & Inter-Component Communication

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

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

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

Access to SharedPreferences Object

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

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

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

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

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

SharedPreferences via PreferenceManager

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

// a SharedPreferences object inside a component

SharedPreferences mSharedPrefs =

PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

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

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

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

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

}

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

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

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

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

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

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

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

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

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

Review: Activity's Lifetimes

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

Persistence of Activity States

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

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

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

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

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

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

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

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

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

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

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

onCreate() & onRestoreInstanceState()

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

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

State Persistence Experiments

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

Experiment 01

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

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

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

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.

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

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.

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

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.

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

2D Animations

Page 29: MobAppDev (Fall 2013): SharedPreferences, Persitence of Activity States, 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

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

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

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

Sample Screenshots & Images

Start Screenshot End of Animation Sample Images in Image Sequences

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

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>

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

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

}

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

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

}