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
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 .
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.
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.
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.
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.
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.
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.
Default arguments : An android.os.Bundle containing default values for the target destination, if supplied. Navigation options : Navigation options, represented as NavOptions .
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With