30
MobiCom on Android Activity Life Cycle & Activity State Persistence Vladimir Kulyukin www.vkedco.blogspot.com http://www.youtube.com/vkedco

MobiCom on Android: Activity Lifecycle & Activity State Persistence

Embed Size (px)

Citation preview

Page 1: MobiCom on Android: Activity Lifecycle & Activity State Persistence

MobiCom on Android

Activity Life Cycle & Activity State Persistence

Vladimir Kulyukin

www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

Page 2: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Outline

● Review of Actvity Lifecycle● Persisting Activity States

– onSaveInstanceState()– onRestoreInstanceState()

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 3: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Review of Activity Lifecycle

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 4: MobiCom on Android: Activity Lifecycle & Activity State Persistence

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

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

Page 5: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Review: Lifecycle Callback Methods

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

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

}

Page 6: MobiCom on Android: Activity Lifecycle & Activity State Persistence

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedcoImage Source: http://developer.android.com/guide/components/activities.html#Lifecycle

Page 7: MobiCom on Android: Activity Lifecycle & Activity State Persistence

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

Review: Activity's Lifetimes

Page 8: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Persisting Activity State

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 9: MobiCom on Android: Activity Lifecycle & Activity State Persistence

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

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

Page 10: MobiCom on Android: Activity Lifecycle & Activity State Persistence

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.vkedco.blogspot.comhttp://www.youtube.com/vkedco

Page 11: MobiCom on Android: Activity Lifecycle & Activity State Persistence

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

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

Page 12: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Online Doc Note on onSaveInstanceState()

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

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

Page 13: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Online Doc Note on onSaveInstanceState()

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#Lifecycle

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

Page 14: MobiCom on Android: Activity Lifecycle & Activity State Persistence

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

onCreate() & onRestoreInstanceState()

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

Page 15: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Activity State Persistence Experiments

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 16: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Sample Application: Tsaritsyno Park Picture Browser

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

image source: http://en.wikipedia.org/wiki/Tsaritsyno_Park

Tsaritsyno Park, Moscow, Russia

Page 17: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Demo: Activity Screen Flow

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

Start Screen Up & Down Buttons Context Menu

source code is here

Page 18: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Internationalization: Strings.xml

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

<string name="title_activity_picture_browser">Tsaritsyno Image Browser</string>

<string name="context_menu_title">Context Menu Options</string>

<string name="context_menu_finish_item">Finish Activity</string>

<string name="context_menu_grayscale_item">Grayscale Image</string>

<string name="context_menu_wiki_en_item">Tsaritsyno Wikipedia</string>

<string name="context_menu_wiki_ru_item">Царицыно Википедия</string>

<string name="wiki_ru_url">http://ru.wikipedia.org/wiki/%D0%A6%D0%B0%D1%80%D0%B8%D1%86%D1%8B%D0%BD%D0%BE_%28%D0%B4%D0%B2%D0%BE%D1%80%D1%86%D0%BE%D0%B2%D0%BE-%D0%BF%D0%B0%D1%80%D0%BA%D0%BE%D0%B2%D1%8B%D0%B9_%D0%B0%D0%BD%D1%81%D0%B0%D0%BC%D0%B1%D0%BB%D1%8C%29</string>

<string name="wiki_en_url">http://en.wikipedia.org/wiki/Tsaritsyno_Park</string>

<string name="img_id_key">img_id</string>

<string name="title_activity_picture_grayscaler">Tsaritsyno Image Grayscaler</string>

Page 19: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Internationalization: Strings.xml

http://www.vkedco.blogspot.comhttp://www.youtube.com/vkedco

public boolean onContextItemSelected(MenuItem item) {

switch ( item.getItemId() ) {

case CONTEXT_MENU_WIKI_EN_ITEM:

Intent wiki_en = new Intent(Intent.ACTION_VIEW);

wiki_en.setData(Uri.parse(mRes.getString(R.string.wiki_en_url)));

startActivity(wiki_en); return true;

case CONTEXT_MENU_WIKI_RU_ITEM:

Intent wiki_ru = new Intent(Intent.ACTION_VIEW);

wiki_ru.setData(Uri.parse(mRes.getString(R.string.wiki_ru_url)));

startActivity(wiki_ru); return true;

case CONTEXT_MENU_GRAYSCALE_ITEM:

saveSharedPrefs();

Intent grayscale = new Intent(this.getApplicationContext(), PictureProcessorAct.class);

startActivity(grayscale); return true;

case CONTEXT_MENU_FINISH_ITEM: this.finish(); return true;

default: return false;

}}

Page 20: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Experiment 01: Question

Which methods are called when the PictureBrowser application is started?

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 21: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Experiment 01: Answer

onCreate(), onStart(), onResume() are called. Nothing unexpected.

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 22: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Experiment 02: Question

Suppose another application is started when PictureBrowserAct is on top of the activity stack. Which methods are called?

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 23: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Experiment 02: Answer

onSaveInstanceState(), onPause(), onPause() are called.

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 24: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Experiment 03: Question

Suppose the user presses the Back button to return to the PictureBrowser application, which methods of PictureBrowserAct will be called?

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 25: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Experiment 03: Answer

onRestart(), onStart(), onResume() are called.

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 26: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Experiment 04: Question

Suppose the user navigates to some image up or down and clicks Finish in the ContextMenu registered for the ImageView of PictureBrowserAct, will PictureBrowserAct.onSaveInstanceState() be called?

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 27: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Experiment 04: Answer

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

Page 28: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Experiment 05: Question

Suppose the saving and loading of shared preferences is enabled in onCreate() and onPause(). Suppose the user navigates to some image up or down and clicks Finish in the ContextMenu registered for the ImageView of PictureBrowserAct, will the image be displayed on re-start if the image number is persisted via SharedPreferences?

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 29: MobiCom on Android: Activity Lifecycle & Activity State Persistence

Experiment 05: Answer

Yes, it will be displayed called, because it was persisted via shared preferences.

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

Page 30: MobiCom on Android: Activity Lifecycle & Activity State Persistence

References

● http://developer.android.com/reference/android/content/SharedPreferences.html● http://developer.android.com/guide/components/activities.html#Lifecycle

http://www.youtube.com/vkedco http://www.vkedco.blogspot.com