I am trying out the new Navigation Architecture Component, and I can't figure out how to do this:
I have 1 Activity (MainActivity) + 3 Fragments:
I would like to use SplashFragment to determine if I should navigate to MainFragment or SignUpFragment, but once it reaches either of those 2, you should not be able to pop back to SplashFragment. How can I do that with the new navigation component?
I tried popBackStack
before and after calling navigate(R.id.action_xxx)
, but neither of them work (which make sense: before it has nothing to pop; after it just closes the fragment that just got added). Does that mean the only way to do that is to override onBackPress
to intercept it and make sure navigateUp
does not get call in those cases?
Thanks!
How do I get rid of current fragment navigation component? You add to the back state from the FragmentTransaction and remove from the backstack using FragmentManager pop methods: FragmentManager manager = getActivity(). getSupportFragmentManager();
Android Jetpack's Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer. The Navigation component also ensures a consistent and predictable user experience by adhering to an established set of principles.
Destinations and actions Basically, the navigation component has three parts, i.e. the navigation graph, the NavHost and the NavController . They are well discussed in this article. In the navigation graph, we have two very important units. destinations: This is a fragment or activity in your application.
First, add attributes app:popUpTo='your_nav_graph_id'
and app:popUpToInclusive="true"
to the action tag.
<fragment android:id="@+id/signInFragment" android:name="com.glee.incog2.android.fragment.SignInFragment" android:label="fragment_sign_in" tools:layout="@layout/fragment_sign_in" > <action android:id="@+id/action_signInFragment_to_usersFragment" app:destination="@id/usersFragment" app:launchSingleTop="true" app:popUpTo="@+id/main_nav_graph" app:popUpToInclusive="true" /> </fragment>
Second, navigate to the destination, using the above action as parameter.
findNavController(fragment).navigate(SignInFragmentDirections.actionSignInFragmentToUserNameFragment())
NOTE: If you navigate using method navigate(@IdRes int resId)
, you won't get the desired result. Hence, I used method navigate(@NonNull NavDirections directions)
.
This worked for me in alpha05 release. Add app:popUpTo="@id/nav_graph" in the action tag(inside your nav_graph.xml file). Here "@id/nav_graph is the id of my graph or also called as the Root.
<navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/nav_graph" app:startDestination="@id/startFragment"> ....... <action android:id="@+id/action_startFragment_to_homeFragment" app:destination="@id/homeFragment" app:popUpTo="@id/nav_graph"/> .......
You can also do this in design tab:- select "SplashFragment" and select the action you want to change and then pick "root" for "Pop To"
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