Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable UP in Navigation for some fragment with the new Navigation Architecture Component?

Tags:

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:

  • SplashFragment (Home)
  • MainFragment
  • SignUpFragment

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!

like image 859
Louis Tsai Avatar asked May 14 '18 17:05

Louis Tsai


People also ask

How do you complete a fragment in navigation component?

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();

What is navigation architecture component?

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.

What are the different navigation components?

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.


2 Answers

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).

like image 79
Subhojit Shaw Avatar answered Oct 15 '22 11:10

Subhojit Shaw


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"

like image 25
Akii Avatar answered Oct 15 '22 11:10

Akii