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

Viewed 1413
2 Answers

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.

You should use viewModel for this problem, the viewmodel will not invoke again and write your api calling code inside viewmodel.

Class TestViewModel:ViewModel(){

 init{
   apiReqest()
}

private fun apiReqest(){
 // write here your api request code and observe it on view using observer
} 

}
Related