Fragments Destroyed/Recreated with Jetpack's Android Navigation Components

Fragments destroyed / recreated with Jetpack's Android Navigation components

Ian Lake from google replied me that we can store the view in a variable and instead of inflating a new layout, just return the instance of pre-stored view on onCreateView()

Source: https://twitter.com/ianhlake/status/1103522856535638016

Leakcanary may show this as leak but its false positive..

Fragment Get Destroyed And Created Again Using Android Jetpack Navigation Controller

As this issues:
Support multiple back stacks for Bottom tab navigation

You can use navigation library version 2.4.0-alpha04 and fragment version 1.4.0-alpha04 for back stacks support.

And yes you should consider using ViewModel to get data that you fetch.

Jetpack Navigation Drawer always recreates the first fragment even in onBackPress

As per the Saving state with fragments guide, it is expected that your Fragment's view (but not the fragment itself) is destroyed and recreated when it is on the back stack.

As per that guide, one of the types of state is non config state:

NonConfig: data pulled from an external source, such as a server or local repository, or user-created data that is sent to a server once committed.

NonConfig data should be placed outside of your fragment, such as in a ViewModel. The ViewModel class inherently allows data to survive configuration changes, such as screen rotations, and remains in memory when the fragment is placed on the back stack.

So your fragment should never be calling fetchAssets("30") in onCreateView(). Instead, this logic should happen inside a ViewModel such that it is instantly available when the fragment returns from the back stack. As per the ViewModel guide, your fetchAssets should be done inside the ViewModel and your Fragment would observe that data.

Navigation resets when switching fragments

Part of Navigation 2.4.0 is support for multiple back stacks - i.e., the ability for each tab of a bottom navigation bar to save its state, restoring that state when you reselect that tab.

As per that blog post:

If you’re using NavigationUI, our set of opinionated helpers for connecting your NavController to Material view components, you’ll find that multiple back stacks is enabled by default for menu items, BottomNavigationView (and now NavigationRailView!), and NavigationView. This means that the common combination of using navigation-fragment and navigation-ui will just work.

This means that if you are using the setupWithNavController methods, then upgrading to Navigation 2.4 will give you support for multiple back stacks immediately. You can verify that by going to your order fragment (thus building a back stack on that first tab), going to a different tab, then tap on the first tab again to reselect it.

Of course, it is your fragments state, not the instances themselves that are saved and restored. This means that each individual fragment must still save its state properly.

Android Navigation Component jump a few steps in graph at once

Just call navigate() multiple times, first to fragment B, then to fragment C. This is in fact precisely how deep links are internally handled by the Navigation Component.



Related Topics



Leave a reply



Submit