Varun A M
Varun A M

Reputation: 1162

Fragments are always recreated when used with Android navigation from jetpack

I am facing this issue of fragments recreation with Android navigation component. I have integrated bottom navigation and coupled it with the android navigation component. So, every time I click on a tab on bottom bar, the fragment is recreated and the old state is not persisted.

I want to retain the state of fragment even when I go to other tabs and come back. I am not finding any solution for it anywhere.

Except bottom navigation, I am using navController.navigate() method to navigate between different fragments.

Upvotes: 16

Views: 10541

Answers (3)

Glebka
Glebka

Reputation: 1678

As alternative you can use hide/show fragment instead of navigation: Show hide fragment in android

Upvotes: 0

SURYA N
SURYA N

Reputation: 293

Hi the problem is fixed in latest version 2.4.0-alpha01 now there is a support multiple backstack navigation

Checkout the link: https://developer.android.com/jetpack/androidx/releases/navigation#version_240_2

Upvotes: 4

kaustubhpatange
kaustubhpatange

Reputation: 532

There is a problem with Navigation to handle multiple back stacks. A similar solution to this problem is available here

You can also use ViewModels to preserve the view state.

Upvotes: 0

Related Questions