How do I disable back navigation and remove the back arrow on a Fragment, using Android Jetpack's Navigation component?

efa303

I am using Google's recommended single activity pattern with Android Jetpack's Navigation component.

Fragment 1 is an authentication screen. After the user is authenticated and navigates to Fragment 2, I would like to make pressing of the Android back button close the app, and remove the back arrow in the app bar.

I have found methods, like onBackPressedDispatcher, to add / remove functionality from the back press, but nothing that also removes the back arrow.

I have also tried app:popUpTo="@+id/firstFragment" when navigating from Fragment 1 to Fragment 2, but that doesn't work either.

This should be possible to specify with a single line of code. Still trying to find. Any tips?

Mohammed Alaa

You need to remove fragment1 from back-stack when navigation to fragment2

fragment1

<fragment
android:id="@+id/fragment1"
android:name="packagenameforFragment1"
android:label="fragment1"
tools:layout="@layout/fragment_1" >
<action
    android:id="@+id/action_Fragment1_to_Fragment2"
    app:destination="@id/Fragment2_id"
    app:launchSingleTop="true"
    app:popUpTo="@+id/your_MainGraph_id"
    app:popUpToInclusive="true" />

then when you navigate from fragment1 to fragment2 call this

findNavController(fragment).navigate(R.id.action_Fragment1_to_Fragment2)

to remove the back button from Fragment2 you can use this

in Activity onCreate()

val appBarConfiguration = AppBarConfiguration
        .Builder(R.id.your_fragment2_id,R.id.any_other_ids_you_want)
        .build()

then setup your toolbar like this

setupActionBarWithNavController(this, yourNavController, appBarConfiguration)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

(Navigation component) How to display back arrow on the activity when back to the home fragment?

how to back to the root fragment or top of hierarchy using Navigation Controller Component in Android?

How do I disable NVDA's arrow key navigation

Using navigation component and menu items how can I get the Up button to go back to previous fragment rather than the "main" fragment

Android Fragment Navigation Custom Navigation Back Button

How to disable Navigation Icon when using Android Jetpack Navigation

Manage toolbar's navigation and back button from fragment to fragment in android

Why I cannot go back to previous fragment with Navigation Component?

Android Navigation component popUpTo taking back to popped up fragment

Android navigation component animate go back from activity to fragment

Conditional back navigation with Android Navigation Component

Android Jetpack Navigation proper back stack with BottomNavigationView

Jetpack navigation: Title and back/up arrow in the action bar?

Removing navigation back arrow in Xamarin.Android

android jetpack navigation instrumented test fail on back navigation

Back Navigation WebView with Fragment

How to position back arrow button in react navigation

Android navigation architecture component - system's back button exits the app

Jetpack navigation with navigation drawer not display back button and title given in the mobile_navigation.xml on child fragment in androidx

How can I use a toolbar in a fragment but not in activity using Jetpack's Navigation

Using Android Jetpack Navigation component with Android BottomAppBar

Back and Up Button with different behavior on Jetpack Navigation Component

How can I change the Font Size and Weight for the Back Arrow and Back Text in a Navigation Page?

Why Android Navigation Component screen not go back to previous Fragment,but a method in onViewCreated of previos Fragment being called?

Back navigation with bottom navigation view and navigation component

Handling back button in Android Navigation Component

How can I remove the text from the navigation bar back item

Popping off screen (from "back stack") represented by a composable to go back to previous screen while using navigation component of Jetpack Compose?

Android architecture component navigation: toolbar back button missing, back not working