Jadu
Jadu

Reputation: 729

Back Navigation Issue (Nav Graph) Issue (android)


I am stuck in this issue of back navigation button for long while, I have tried different methods but still the problem is not resolved

Here is the IMAGE

enter image description here


This is my issue

Here are some codes

Defined in SelectLanguageFragment

 private fun checkUserExist(){
    val currentUser = auth.currentUser
    val navOptions: NavOptions = NavOptions.Builder()
        .setPopUpTo(`in`.jadu.anjuconsumerapp.R.id.consumerHomeFragment, true)
        .build()
    if(currentUser != null){
        findNavController().navigate(`in`.jadu.anjuconsumerapp.R.id.action_selectLanguageFragment_to_consumerHomeFragment)
    }
}

Tried navOptions but didn't worked

Here is my NavGraph NavGraph


NavGraphCodes

app:startDestination="@id/selectLanguageFragment"

 <fragment
    android:id="@+id/selectLanguageFragment"
    android:name="in.jadu.anjuconsumerapp.consumer.commonuis.SelectLanguageFragment"
    android:label="Select Language" >
    <action
        android:id="@+id/action_selectLanguageFragment_to_phoneVerificationFragment"
        app:destination="@id/phoneVerificationFragment" />
    <action
        android:id="@+id/action_selectLanguageFragment_to_consumerHomeFragment"
        app:destination="@id/consumerHomeFragment"
        app:popUpTo="@id/selectLanguageFragment"
        app:popUpToInclusive="true" />
</fragment>

I have tried getSupportActionBar().setDisplayHomeAsUpEnabled(false) but i didn't need this i just want to remove it from the navigation,

I want to Remove this back button from the home screen when user is logged in

Thank you

Upvotes: 0

Views: 122

Answers (1)

Jadu
Jadu

Reputation: 729

So after struggling a lot watched a lot many yt videos, github I finally got the solution from androidx Docs Reference

So, When we are setting up the navigate up ...

  1. Only the Top Level Ids will not have the back arrow
  2. So we need to define the dashboard fragment as a top level or start id (languagefragment is only the start previously)

code

val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController
        val appBarConfiguration = AppBarConfiguration(
            topLevelDestinationIds = setOf(R.id.selectLanguage,R.id.farmerDashboard),
            fallbackOnNavigateUpListener = ::onSupportNavigateUp
        )
        setupActionBarWithNavController(navController,appBarConfiguration)

Here i have declared both the fragments (selectLanguage,farmerDashboard) as top level so that i would not get the back button on these ids the rest app works fine ...

So , this was a simple thing

Hope this helps you :)

Upvotes: 1

Related Questions