Zohaib Akram
Zohaib Akram

Reputation: 627

Android Navigation Components

I added Android new Navigation Component inside my app and created navGraph and added few fragments as a destinations. I figure out every time i navigates into destinations previous destination (fragment) is begin restarted. And my last state of previous destination (fragment) has lost. Is there any options to retain this? As i already used retainInstance in fragment.

Upvotes: 1

Views: 285

Answers (1)

Gal
Gal

Reputation: 422

I have been using the navigation component with fragments in my project and I found that the easiest way to save a fragment state is using the view model component: https://developer.android.com/topic/libraries/architecture/viewmodel?gclid=EAIaIQobChMI442XtIuR4wIVhuiaCh0uGAFZEAAYASAAEgIRIfD_BwE

I have been using MVP Architecture and in my model I extend the view model. That way as long as your activity wasn't destroyed the model instance is being saved and you can access it when the fragment reload so all it's parameters are still there so you can reinitialise your fragment with the saved values inside your model.

Upvotes: 1

Related Questions