How can I access "androidx.savedstate.ViewTreeSavedStateRegistryOwner" in Jetpack Compose 1.2.0?

1.1k Views Asked by At

I was able to access the below when I'm in Jetpack Compose 1.1.1

import androidx.savedstate.ViewTreeSavedStateRegistryOwner

However, when switching over to Jetpack Compose 1.2.0, I can no longer access it. It errors out stating

Unresolved reference: ViewTreeSavedStateRegistryOwner

e.g. In the view, I'm using it as below

ViewTreeSavedStateRegistryOwner.set(this, ViewTreeSavedStateRegistryOwner.get(composeView))

Is there any way I can still access it?

2

There are 2 best solutions below

1
Elye On

In Jetpack Compose 1.2.0, we should use the below instead

import androidx.savedstate.findViewTreeSavedStateRegistryOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner

e.g. in the view

setViewTreeSavedStateRegistryOwner(composeView.findViewTreeSavedStateRegistryOwner())
0
Milan Sheth On

At first for me, it was a bit confusing and after spending an hour I learn how to implement it.

First of all, you don't need to import anything as nowadays Android Studio itself adds the supported library. But I am specifying just for your reference if anyone has not enabled the autoImport feature in Android studio:

import androidx.savedstate.setViewTreeSavedStateRegistryOwner

Here is a complete example below:

val views = ComposeView(context = context)
views.setContent {
      Your @Composable function/component
}
views.setViewTreeSavedStateRegistryOwner(LocalSavedStateRegistryOwner.current)