Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android navigation component resets to start destination after configuration change

I am using the Android navigation component and I have an activity with three fragments if I am in the second fragment and rotate the screen forcing the activity to restart the navigation is returned to the start destination.

shouldn't the navhostFragment retain the state of the graph when the activity restarts?

or is what is happening the default behavior here?

I don't want to add the following even though adding it "works"

 android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"

because I don't want to handle orientation changes myself, I want it to be handled by the system normally and still retain the navigation state

I will provide some of my code if that helps

in the activity I use navController.setGraph() so I can pass data to the start destination like this

 navController = Navigation.findNavController(
        this,
        R.id.nav_host_fragment
    )
 setSupportActionBar(findViewById(R.id.toolbar))
 appBarConfiguration = AppBarConfiguration.Builder(navController.graph).build()
 supportActionBar?.setDisplayHomeAsUpEnabled(true)

 intent.putExtra("EXTRA_KEY","some_data")
 navController.setGraph(R.navigation.nav_graph,intent.extras)

and I navigate from fragment to fragment using this

navController.navigate(FirstFragmentDirections.actionFirstFragmentToSecondFragment())

here is the code in the nav_graph

<fragment
    android:id="@+id/FirstFragment"
    android:name="com.example.app.presentation.ui.FirstFragment"
    android:label="FirstFragment" >
    <action
        android:id="@+id/action_FirstFragment_to_secondFragment"
        app:destination="@id/secondFragment"
        app:enterAnim="@anim/enter_from_right"
        app:exitAnim="@anim/exit_to_left"
        app:popEnterAnim="@anim/enter_from_left"
        app:popExitAnim="@anim/exit_to_right"
        />
</fragment>
<fragment
    android:id="@+id/secondFragment"
    android:name="com.example.app.presentation.ui.secondFragment"
    android:label="secondFragment"
    tools:layout="@layout/fragment_second" />

any help is appreciated thank you

like image 250
amjad masri Avatar asked Jun 02 '20 10:06

amjad masri


People also ask

What is popUpToInclusive?

app:popUpToInclusive="true"/> </fragment> </navigation> When the navigation graph is inflated, these actions are parsed, and corresponding NavAction objects are generated with the configurations defined in the graph. For example, action_b_to_a is defined as navigating from destination b to destination a .

What is safe args in navigation android?

SafeArgs is a gradle plugin that allows you to Pass data to destination UI components. It generates simple object and builder classes for type-safe navigation and access to any associated arguments. Safe Args is strongly recommended for navigating and passing data because it ensures type-safety.

What is Navigation SafeArgs?

SafeArgs is a gradle plugin that allows you to enter information into the navigation graph about the arguments that you want to pass. It then generates code for you that handles the tedious bits of creating a Bundle for those arguments and pulling those arguments out of the Bundle on the other side.

What is NavController in Android?

NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.

Can the Android navigation architecture components support multiple start destinations?

The Android Navigation Architecture Components currently only support a single start destination which doesn’t always make sense when using drawer style navigation which can have multiple destinations that are on the same level.

How do I create a navigation fragment in Android Studio?

In the Navigation Editor, click the New Destination icon, and then click Create new destination. In the New Android Component dialog that appears, create your fragment. For more information on fragments, see the fragment documentation. Back in the Navigation Editor, notice that Android Studio has added this destination to the graph.

How do I add a new destination to a fragment?

To add a new destination using the Navigation Editor, do the following: In the Navigation Editor, click the New Destination icon, and then click Create new destination. In the New Android Component dialog that appears, create your fragment. For more information on fragments, see the fragment documentation.

What are default arguments and navigation options in Android?

Default arguments : An android.os.Bundle containing default values for the target destination, if supplied. Navigation options : Navigation options, represented as NavOptions .


1 Answers

You should generally never need to call setGraph() yourself, but you can workaround it like so in this particular case (and it will actually still work as you expect, because NavController / Navigator restores state properly across config changes and process death automatically):

if (savedInstanceState == null) {
    navController.setGraph(R.navigation.nav_graph,intent.extras)
}
like image 75
EpicPandaForce Avatar answered Oct 19 '22 05:10

EpicPandaForce