Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Jetpack Compose by rememberSaveable State does not survive Back Button

I cannot find the savedInstanceState function shown in code examples in the Restore UI state after activity or process recreation section of the State and Jetpack Compose that the section says "retains state across activity and process recreation."

I did find the androidx.compose.runtime.saveable documentation which contains rememberSaveable which seems to be the renaming or replacement of savedInstanceState and its documentation also says "... the stored value will survive the activity or process recreation."

However, when I use it in my code, the state does not survive the back button, although it does survive rotations. That's contrary to what the documentation says.

package com.example.jetwatch

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.material.*
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue

class MainActivity : AppCompatActivity() {
 override fun onCreate(bundle: Bundle?) {
  super.onCreate(bundle)
  setContent {
   MaterialTheme(colors = if (isSystemInDarkTheme()) darkColors() else lightColors()) {
    Surface {
     Column {
      Row {
       Column {
        var m by rememberSaveable { mutableStateOf(0) }
        Text("m = $m")
        Button(onClick = { ++m }) {
         Text("bump")
        }
       }
      }
     }
    }
   }
  }
 }
}
like image 211
user2297550 Avatar asked Mar 01 '21 15:03

user2297550


People also ask

Should I pass ViewModel to composable?

We recommend screen-level composables use ViewModel instances for providing access to business logic and being the source of truth for their UI state. You should not pass ViewModel instances down to other composables.

What is recomposition in jetpack compose?

Recomposition is when Jetpack Compose re-executes the composables that may have changed in response to state changes, and then updates the Composition to reflect any changes. A Composition can only be produced by an initial composition and updated by recomposition.

How do you use Viewmodels in jetpack compose?

There are two ways to declare the data within a ViewModel so that it is observable. One option is to use the Compose state mechanism which has been used extensively throughout this book. An alternative approach is to use the Jetpack LiveData component, a topic that will be covered later in this chapter.

What is remember in compose Android?

Compose provides remember() , a function you can use to store a single object in memory. During the first composition run, remember() stores the initial value. In each recomposition, remember() returns the stored value so the composable can use it.

What is Jetpack compose for Android apps?

Jetpack Compose helps you be explicit about where and how you store and use state in an Android app. This guide focuses on the connection between state and composables, and on the APIs that Jetpack Compose offers to work with state more easily.

Do I need mutablestate<T> in jetpack compose?

For this, you must use rememberSaveable. rememberSaveable automatically saves any value that can be saved in a Bundle. For other values, you can pass in a custom saver object. Other supported types of state Jetpack Compose doesn't require that you use MutableState<T> to hold state.

How does the composable in jetpack compose update itself?

The composable in Jetpack Compose updates itself based on the value of the state. When the value is changed, the composable function only re-composes the composable whose data has been changed and ignores the others.

What happens when the state variable changes in jetpack compose?

When the State Variable changes, it triggers only the particular UI that interacts with the State Variable to be redrawn (recompose). There’s another concept important for Jetpack Compose, that’s Remember. Before we understand Remember, let’s relook into Recomposition. override fun onCreate (savedInstanceState: Bundle?) {


Video Answer


1 Answers

the state does not survive the back button

In that sample, this is expected behavior. The default behavior of back navigation is to destroy the activity. Saved instance state is discarded at this point. This is not unique to Compose and has been stock Android behavior since Android 1.0.

That's contrary to what the documentation says.

"Process termination", as used in the documentation, refers to this flow:

  • User is in your app
  • User switches to the launcher (via home navigation) or to another app (e.g., via the overview screen)
  • Over the course of the next several minutes (but fewer than 30), Android terminates your process while it is in the background
  • Use returns to your app by one means or another

At that point, Android will fork a fresh process for you and try to restore your UI to where you had been before the user switched away from your app. The saved instance state is part of that restoration.

like image 164
CommonsWare Avatar answered Sep 27 '22 18:09

CommonsWare