deep dive into android state restoration · the story of a newbie android developer. kevin has just...
TRANSCRIPT
![Page 1: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/1.jpg)
Deep Dive Into Android State Restoration
![Page 2: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/2.jpg)
@cyrilmottier !WEBSITE
cyrilmottier.com
![Page 3: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/3.jpg)
![Page 4: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/4.jpg)
![Page 5: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/5.jpg)
![Page 6: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/6.jpg)
![Page 7: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/7.jpg)
The story of a newbie Android developer
![Page 8: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/8.jpg)
Kevin has just developed his first
Android app
![Page 9: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/9.jpg)
He discovers an annoying bug: Fields are cleared on rotate
![Page 10: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/10.jpg)
3 options
![Page 11: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/11.jpg)
3 optionsDon’t care
![Page 12: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/12.jpg)
3 optionsDon’t care Block orientation
![Page 13: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/13.jpg)
3 optionsDon’t care Block orientation Use configChanges
Hint: all options are
![Page 14: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/14.jpg)
1 <activity!2 android:name=".HomeActivity"!3 android:configChanges="orientation">!4 !5 <!-- Some sweet IntentFilters. -->!6 !7 </activity>
![Page 15: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/15.jpg)
Kevin’s satisfied
![Page 16: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/16.jpg)
Still having issues on…
![Page 17: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/17.jpg)
language changesStill having issues on…
![Page 18: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/18.jpg)
1 <activity!2 android:name=".HomeActivity"!3 android:configChanges="orientation!4 !5 <!-- Some annoying IntentFilters. -->!6 !7 </activity>
|locale">
![Page 19: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/19.jpg)
ANGRY!Angry Kevin is
![Page 20: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/20.jpg)
1 <activity!2 android:name=".HomeActivity"!3 android:configChanges=“orientation|locale|! mcc|mnc|touchscreen|keyboard|! keyboardHidden|navigation|uiMode|! screenLayout|fontScale|screenSize|! smallestScreenSize">!4 !5 <!-- Some fuc**** IntentFilters. Arrggh! -->!6 !7 </activity>
![Page 21: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/21.jpg)
The nightmare continues… Still having issues when moving the app to the background
![Page 22: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/22.jpg)
God save the
STATE
![Page 23: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/23.jpg)
State restoration key components
![Page 24: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/24.jpg)
The container Parcel
![Page 25: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/25.jpg)
Parcelable
The container Parcel
The content
Primitives typesPrimitives arrays
![Page 26: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/26.jpg)
The content
Primitives typesPrimitives arrays
1 parcel.writeInt(1);!2 parcel.writeLong(2L);!3 parcel.writeFloat(3F);!4 parcel.writeString("Hi!");
Parcelable
![Page 27: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/27.jpg)
1 parcel.writeIntArray(new int[]{1, 2, 3});!2 parcel.writeLongArray(new long[]{1L, 2L, 3L});!3 parcel.writeDoubleArray(new double[]{1, 2, 3});!4 parcel.writeStringArray(new String[]{!5 "Hi", "Droidcon", "guys!"!6 });
The content
Primitives typesPrimitives arrays
Parcelable
![Page 28: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/28.jpg)
1 public final class Suggestion implements Parcelable {! 2 ! 3 public final String id;! 4 public final String name;! 5 public final int type;! 6 ! 7 public Suggestion(String id, String name, int type) {! 8 this.id = Objects.requireNonNull(id);! 9 this.name = Objects.requireNonNull(name);!10 this.type = type;!11 }!12 !13 }
![Page 29: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/29.jpg)
1 @Override! 2 public int describeContents() {! 3 return 0;! 4 }! 5 ! 6 @Override! 7 public void writeToParcel(Parcel dest, int flags) {! 8 dest.writeString(id);! 9 dest.writeString(name);!10 dest.writeInt(type);!11 }!12 13 public static final Parcelable.Creator<Suggestion> CREATOR = !14 new Parcelable.Creator<Suggestion>() {!15 public Suggestion createFromParcel(Parcel in) {!16 return new Suggestion(in.readString(), // !17 in.readString(), //!18 in.readInt());!19 }!20 !21 public Suggestion[] newArray(int size) {!22 return new Suggestion[size];!23 }!24 };
![Page 30: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/30.jpg)
Parcelable.Creator!The base creator interface
!
Parcelable.ClassLoaderCreator!A creator with the ClassLoader passed on read.
!
ParcelableCompat & ParcelableCompatCreatorCallbacks!Compatibility stuff
![Page 31: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/31.jpg)
Bundle A key-value map & type-safe Parcelable
![Page 32: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/32.jpg)
Parcel!internally uses reflection
(required to get the CREATOR instance)
…i.e. beware Proguard
![Page 33: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/33.jpg)
Activity level state restoration
![Page 34: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/34.jpg)
onCreate(null)
![Page 35: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/35.jpg)
onCreate(null)
![Page 36: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/36.jpg)
: non-null Bundle
onSaveInstanceState( )
onCreate(null)
![Page 37: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/37.jpg)
onSaveInstanceState( )
onCreate( )
onCreate(null)
: non-null Bundle
![Page 38: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/38.jpg)
onSaveInstanceState( )
onCreate( )
onRestoreInstanceState( )
onCreate(null)
: non-null Bundle
![Page 39: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/39.jpg)
onSaveInstanceState( )
onCreate( )
onRestoreInstanceState( )
onCreate(null)
: non-null Bundle
![Page 40: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/40.jpg)
onSaveInstanceState( )
onCreate( )
onRestoreInstanceState( )
onCreate(null)
: non-null Bundle
![Page 41: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/41.jpg)
What to save?Non persistent or non reconstructible info
![Page 42: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/42.jpg)
1 public class SearchActivity extends Activity {! 2 ! 3 private static final String STATE_OUTWARD = "state:outward";! 4 ! 5 private DateComponents mOutward;! 6 ! 7 @Override! 8 protected void onCreate(Bundle inState) {! 9 super.onCreate(inState);!10 !11 if (inState != null) {!12 DateComponents components = inState.getParcelable(STATE_OUTWARD);!13 if (components != null) {!14 setOutward(components);!15 }!16 }!17 }!18 !19 @Override!20 protected void onSaveInstanceState(Bundle outState) {!21 super.onSaveInstanceState(outState);!22 outState.putParcelable(STATE_OUTWARD, mOutward);!23 }!24 }
![Page 43: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/43.jpg)
onSaveInstanceState savesWindow
![Page 44: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/44.jpg)
Window Fragments
onSaveInstanceState saves
![Page 45: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/45.jpg)
Window Fragments Dialogs
onSaveInstanceState saves
![Page 46: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/46.jpg)
Always call the SUPER METHODS Android has no guards on save-related methods
![Page 47: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/47.jpg)
android:stateNotNeededFor restart-on-crash apps only
(i.e. launcher app)
![Page 48: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/48.jpg)
Developer options Don’t keep activities
![Page 49: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/49.jpg)
Developer options Don’t keep activities
![Page 50: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/50.jpg)
View level state restoration
![Page 51: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/51.jpg)
Android saves UI state AUTOMAGICALLY
![Page 52: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/52.jpg)
Android saves UI state AUTOMAGICALLY
(aka “It just works!™”)
![Page 53: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/53.jpg)
Android saves UI state AUTOMAGICALLY
(aka “It just works!™”)
…except in some cases
![Page 54: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/54.jpg)
Works out-of-the-box if Views
1. Have an ID
2. Are “save” enabled
3. Come from the framework
![Page 55: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/55.jpg)
saveHierarchyState()It always begins with a call to
![Page 56: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/56.jpg)
EditText!@id/text
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
![Page 57: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/57.jpg)
EditText!@id/text
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
SparseArray<Parcelable>
![Page 58: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/58.jpg)
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
SparseArray<Parcelable>
S1
EditText!@id/text
![Page 59: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/59.jpg)
@id/container
SparseArray<Parcelable>
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
S1
EditText!@id/text
![Page 60: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/60.jpg)
EditText!@id/text
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
@id/container S1
S2 SparseArray<Parcelable>
![Page 61: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/61.jpg)
@id/text
@id/container S1
S2
SparseArray<Parcelable>
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
EditText!@id/text
![Page 62: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/62.jpg)
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView@id/text
@id/container S1
S2
SparseArray<Parcelable>
S3
EditText!@id/text
![Page 63: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/63.jpg)
@id/text
@id/container
@id/check_box
S1
S2
S3
SparseArray<Parcelable>
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
EditText!@id/text
![Page 64: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/64.jpg)
Controlling save
setSaveEnabled(boolean)
setSaveFromParentEnabled(boolean)
![Page 65: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/65.jpg)
restoreHierarchyState()It always ends with a call to
![Page 66: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/66.jpg)
@id/container S1
@id/text S2
@id/check_box S3
SparseArray<Parcelable>
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
EditText!@id/text
![Page 67: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/67.jpg)
@id/container S1
S2
S3
@id/text
@id/check_box
SparseArray<Parcelable>
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
EditText!@id/text
![Page 68: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/68.jpg)
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
@id/container S1
S2
@id/check_box S3S1
@id/text
SparseArray<Parcelable>
EditText!@id/text
![Page 69: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/69.jpg)
@id/container S1
@id/text S2
@id/check_box S3
SparseArray<Parcelable>
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
EditText!@id/text
![Page 70: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/70.jpg)
EditText!@id/text
RelativeLayout!@id/container
CheckBox!@id/check_box
TextViewS2
@id/container S1
@id/text
@id/check_box S3
S2 SparseArray<Parcelable>
![Page 71: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/71.jpg)
@id/container S1
@id/text S2
@id/check_box S3
SparseArray<Parcelable>
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
EditText!@id/text
![Page 72: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/72.jpg)
RelativeLayout!@id/container
CheckBox!@id/check_box
TextView
@id/container S1
@id/text S2
@id/check_box S3
S3
SparseArray<Parcelable>
EditText!@id/text
![Page 73: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/73.jpg)
Ensure your Views’ IDs are unique & constant
![Page 74: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/74.jpg)
1 static class SavedState extends BaseSavedState {! 2 int checked;! 3 ! 4 SavedState(Parcelable superState) { super(superState); }! 5 ! 6 private SavedState(Parcel in) {! 7 super(in);! 8 checked = in.readInt();! 9 }!10 !11 @Override!12 public void writeToParcel(Parcel out, int flags) {!13 super.writeToParcel(out, flags);!14 out.writeInt(checked);!15 }!16 !17 public static final Parcelable.Creator<SavedState> CREATOR = !18 new Parcelable.Creator<SavedState>() {!19 public SavedState createFromParcel(Parcel in) {!20 return new SavedState(in);!21 }!22 public SavedState[] newArray(int size) {!23 return new SavedState[size];!24 }!25 };!26 }
![Page 75: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/75.jpg)
1 @Override! 2 public Parcelable onSaveInstanceState() {! 3 final Parcelable superState = super.onSaveInstanceState();! 4 SavedState ss = new SavedState(superState);! 5 ss.checked = isChecked() ? 1 : 0;! 6 return ss;! 7 }! 8 ! 9 @Override!10 public void onRestoreInstanceState(Parcelable state) {!11 SavedState ss = (SavedState) state;!12 super.onRestoreInstanceState(ss.getSuperState());!13 setChecked(ss.checked == 1);!14 }
![Page 76: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/76.jpg)
FrameLayout!@id/root
ImageBox!@id/container1
CheckBox!@id/check_box
ImageView!@id/image
ImageBox!@id/container2
CheckBox!@id/check_box
ImageView!@id/image
![Page 77: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/77.jpg)
FrameLayout!@id/root
ImageBox!@id/container1
ImageView!@id/image
CheckBox!@id/check_box
ImageBox!@id/container2
ImageView!@id/image
CheckBox!@id/check_box
Custom views with children with same IDs
![Page 78: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/78.jpg)
1 static class SavedState extends BaseSavedState {! 2 ! 3 SparseArray childrenStates;! 4 ! 5 SavedState(Parcelable superState) { super(superState); }! 6 ! 7 private SavedState(Parcel in, ClassLoader loader) {! 8 super(in);! 9 childrenStates = in.readSparseArray(loader);!10 }!11 !12 @Override!13 public void writeToParcel(Parcel out, int flags) {!14 super.writeToParcel(out, flags);!15 out.writeSparseArray(childrenStates);!16 }!17 !18 public static final Creator<SavedState> CREATOR = ParcelableCompat.!19 newCreator(new ParcelableCompatCreatorCallbacks<SavedState>() {!20 @Override!21 public SavedState createFromParcel(Parcel source, ClassLoader loader) {!22 return new SavedState(source, loader);!23 }!24 @Override!25 public SavedState[] newArray(int size) {!26 return new SavedState[size];!27 }!28 });!29 }
![Page 79: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/79.jpg)
1 @Override! 2 public Parcelable onSaveInstanceState() {! 3 final Parcelable superState = super.onSaveInstanceState();! 4 SavedState ss = new SavedState(superState);! 5 ss.childrenStates = new SparseArray<Parcelable>();! 6 for (int i = 0; i < getChildCount(); i++) {! 7 getChildAt(i).saveHierarchyState(ss.childrenStates);! 8 }! 9 return ss;!10 }!11 !12 @Override!13 public void onRestoreInstanceState(Parcelable state) {!14 SavedState ss = (SavedState) state;!15 super.onRestoreInstanceState(ss.getSuperState());!16 for (int i = 0; i < getChildCount(); i++) {!17 getChildAt(i).restoreHierarchyState(ss.childrenStates);!18 }!19 }
![Page 80: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/80.jpg)
That has solved nothing! Still need to block save/restore dispatch
![Page 81: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/81.jpg)
1 @Override!2 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {!3 dispatchFreezeSelfOnly(container);!4 }!5 !6 @Override!7 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {!8 dispatchThawSelfOnly(container);!9 }
![Page 82: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/82.jpg)
Fragment level state restoration
![Page 83: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/83.jpg)
Very similar to Activities state restoration lifecycle.
(Fragments are tied to Activity after all)
![Page 84: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/84.jpg)
Fragment blocks Activity save mechanism
with framework setSaveFromParentEnabled(false)
with support library NoSaveStateFrameLayout
![Page 85: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/85.jpg)
2 distinct states
Fragment + View common case
View only detach, addToBackStack, etc.
![Page 86: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/86.jpg)
Can to be used to create smooth transitions between your Activities:
!
- Save the state SA of A - Start B with no animations passing SA
- Apply SA to B - Transition between A and B was smooth
Leveraging save/restore
![Page 87: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/87.jpg)
Summarizing in three rules
![Page 88: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/88.jpg)
Always save the state An Android app must survive configuration changes & low memory conditions.
![Page 89: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/89.jpg)
Only save essential info Only save info that is non persistent
or can not be reconstructed later.
![Page 90: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/90.jpg)
Use correct levels Save instance states at the appropriate component level: Activity, Fragment or View.
![Page 91: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/91.jpg)
Thank you! @cyrilmottier
cyrilmottier.com
![Page 92: Deep Dive Into Android State Restoration · The story of a newbie Android developer. Kevin has just developed his first Android app. He discovers an annoying bug: Fields are cleared](https://reader033.vdocument.in/reader033/viewer/2022043000/5f77860666ada859126b19c7/html5/thumbnails/92.jpg)
Resources Dressed for Iceland • Cécile Bernard Moelwynion, Eryri, Cymru • Marc Poppleton Happy, Confused, Wink, Sad, Angry • Megan Sheehan Floppy-Disk • Alex Auda Samora
Fonts Source Sans Pro
Courier