Android navigation component: how save fragment state

后端 未结 4 1737
灰色年华
灰色年华 2020-12-03 01:15

I use bottomNavigationView and navigation component. Please tell me how I don\'t destroy the fragment after switching to another tab and return to the old one? For example I

4条回答
  •  悲&欢浪女
    2020-12-03 01:28

    In case you can deal with destroying fragment, but want to save ViewModel, you can scope it into the Navigation Graph:

    private val viewModel: FavouritesViewModel by 
        navGraphViewModels(R.id.mobile_navigation) {
            viewModelFactory
        }
    

    Read more here

提交回复
热议问题