Slide 1

Slide 1 text

Where Did My State Go? Subhrajyoti Sen @iamsubhrajyoti

Slide 2

Slide 2 text

This is a Lifecycle talk

Slide 3

Slide 3 text

The First thing

Slide 4

Slide 4 text

The First thing We have all done this

Slide 5

Slide 5 text

The First thing We have all done this

Slide 6

Slide 6 text

Is this Enough?

Slide 7

Slide 7 text

NO

Slide 8

Slide 8 text

NO

Slide 9

Slide 9 text

But WHY?

Slide 10

Slide 10 text

Reasons

Slide 11

Slide 11 text

Reasons 1. Dark Theme

Slide 12

Slide 12 text

Reasons 1. Dark Theme 2. Multi-Window

Slide 13

Slide 13 text

Reasons 1. Dark Theme 2. Multi-Window 3. Process Death

Slide 14

Slide 14 text

Dark Theme

Slide 15

Slide 15 text

Dark Theme Introduced in Android 10

Slide 16

Slide 16 text

Dark Theme Introduced in Android 10 Can be changed by 1. System Toggle 2. AppCompat in-app

Slide 17

Slide 17 text

Dark Theme

Slide 18

Slide 18 text

Dark Theme Triggers a uiMode configuration change

Slide 19

Slide 19 text

Dark Theme Triggers a uiMode configuration change All started Activities are automatically recreated.

Slide 20

Slide 20 text

Dark Theme

Slide 21

Slide 21 text

Dark Theme This will invoke onConfigurationChanged() and not recreate all started Activities.

Slide 22

Slide 22 text

Multi-Window

Slide 23

Slide 23 text

Multi-Window

Slide 24

Slide 24 text

Multi-Window

Slide 25

Slide 25 text

Multi-Window Introduced in Android N

Slide 26

Slide 26 text

Multi-Window Introduced in Android N A slight change in Lifecycle.

Slide 27

Slide 27 text

Multi-Window Introduced in Android N A slight change in Lifecycle. You can’t completely depend on onPause() and onResume()

Slide 28

Slide 28 text

Multi-Window Introduced in Android N A slight change in Lifecycle. You can’t completely depend on onPause() and onResume() screenOrientation will be ignored

Slide 29

Slide 29 text

Multi-Window Portrait orientation != Vertical device

Slide 30

Slide 30 text

Multi-Window Portrait orientation != Vertical device android:resizeableActivity="false" to opt out of Multi-Window

Slide 31

Slide 31 text

Process Death

Slide 32

Slide 32 text

Process Death ● The device is running low on memory and the OS kills your app to free up memory.

Slide 33

Slide 33 text

Process Death ● The device is running low on memory and the OS kills your app to free up memory. ● It happens if your app is in the Paused/Stopped state.

Slide 34

Slide 34 text

Process Death ● The device is running low on memory and the OS kills your app to free up memory. ● It happens if your app is in the Paused/Stopped state. ● Cannot work around it.

Slide 35

Slide 35 text

Process Death - Testing 1. Developer Options -> Apps -> Background process limit 2. Choose ‘No background processes’ 3. Open your app and put in the background 4. Open another app and switch to your app

Slide 36

Slide 36 text

Process Death - Testing 1. Put the app in background 2. adb shell am kill packagename

Slide 37

Slide 37 text

ViewModel

Slide 38

Slide 38 text

ViewModel ViewModels persist state across Activity recreation. ViewModels do not persist state on process death.

Slide 39

Slide 39 text

ViewModel ViewModels persist state across Activity recreation. ViewModels do not persist state on process death. Use SavedStateHandle to save state.

Slide 40

Slide 40 text

ViewModel - SavedStateHandle

Slide 41

Slide 41 text

ViewModel - SavedStateHandle val viewModel = ViewModelProvider(this, SavedStateVMFactory(this)) .get(MyViewModel::class.java)

Slide 42

Slide 42 text

ViewModel - SavedStateHandle val viewModel = ViewModelProvider(this, SavedStateVMFactory(this)) .get(MyViewModel::class.java) class MyViewModel(val state : SavedStateHandle) : ViewModel() { fun saveProductId(productId: String) { state.set("productId", productId) } fun getProductId(): String { return state.get("productId")?: "" } }

Slide 43

Slide 43 text

Custom View Views can save their own state. Views need to have an ID to be able to save state.

Slide 44

Slide 44 text

Custom View private class SavedState : BaseSavedState { var value = 1 constructor(superState: Parcelable) : super(superState) private constructor(source: Parcel) : super(source) { value = source.readInt() } override fun writeToParcel(out: Parcel, flags: Int) { super.writeToParcel(out, flags) out.writeInt(value) } }

Slide 45

Slide 45 text

Custom View private class SavedState : BaseSavedState { //.. companion object { val CREATOR: Parcelable.Creator = object : Parcelable.Creator { override fun createFromParcel(source: Parcel) = SavedState(source) override fun newArray(size: Int) = arrayOfNulls(size) } } }

Slide 46

Slide 46 text

Custom View class CustomView(context: Context) : View(context) { var value = 1 override fun onSaveInstanceState(): Parcelable { val superState = super.onSaveInstanceState() val state = SavedState(superState) state.value = this.value return state } }

Slide 47

Slide 47 text

Custom View class CustomView(context: Context) : View(context) { override fun onRestoreInstanceState(state: Parcelable) { val savedState = state as SavedState super.onRestoreInstanceState(savedState.superState) this.value = savedState.value } }

Slide 48

Slide 48 text

What about TESTING?

Slide 49

Slide 49 text

Testing Activity/Fragment Recreation

Slide 50

Slide 50 text

Testing Activity/Fragment Recreation Can be done with Espresso

Slide 51

Slide 51 text

Testing Activity/Fragment Recreation Can be done with Espresso Easier with AndroidX Test

Slide 52

Slide 52 text

Testing Activity/Fragment Recreation Can be done with Espresso Easier with AndroidX Test debugImplementation 'androidx.fragment:fragment-testing:$fragment_version'

Slide 53

Slide 53 text

Testing Fragment Recreation

Slide 54

Slide 54 text

Testing Fragment Recreation @RunWith(AndroidJUnit4::class) class MyTestSuite { @Test fun testRecreation() { val scenario = launchFragmentInContainer() } }

Slide 55

Slide 55 text

Testing Fragment Recreation @RunWith(AndroidJUnit4::class) class MyTestSuite { @Test fun testRecreation() { val scenario = launchFragmentInContainer() // perform actions } }

Slide 56

Slide 56 text

Testing Fragment Recreation @RunWith(AndroidJUnit4::class) class MyTestSuite { @Test fun testRecreation() { val scenario = launchFragmentInContainer() // perform actions scenario.recreate() } }

Slide 57

Slide 57 text

Testing Fragment Recreation @RunWith(AndroidJUnit4::class) class MyTestSuite { @Test fun testRecreation() { val scenario = launchFragmentInContainer() // perform actions scenario.recreate() // test events } }

Slide 58

Slide 58 text

Testing Fragment Lifecycle

Slide 59

Slide 59 text

Testing Fragment Lifecycle @RunWith(AndroidJUnit4::class) class MyTestSuite { @Test fun testLifecycle() { val scenario = launchFragmentInContainer() } }

Slide 60

Slide 60 text

Testing Fragment Lifecycle @RunWith(AndroidJUnit4::class) class MyTestSuite { @Test fun testLifecycle() { val scenario = launchFragmentInContainer() scenario.moveToState(State.CREATED) } }

Slide 61

Slide 61 text

Thank You @iamsubhrajyoti