How to use popUpToSaveState and restoreState to save and keep state in Navigation Component Android Kotlin?

3.2k Views Asked by At

I am using Navigation Component to handle navigations, but when use popBackStack() or onBackPressed() to back to previous fragment my onViewCreated call again in fragment with api and set up views.
Since Navigation Component 2.4 unveiled new features (popUpToSaveState and restoreState), i want to use it. please tell me how?

1

There are 1 best solutions below

0
vignesh On

Even though you save your state onViewCreated will be called.

you need to use to save state.

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
}

and restore it on onCreate.