Actionbar Up Navigation with Fragments

actionbar up navigation with fragments

Implement OnBackStackChangedListener and add this code to your Fragment Activity.

@Override
public void onCreate(Bundle savedInstanceState) {
//Listen for changes in the back stack
getSupportFragmentManager().addOnBackStackChangedListener(this);
//Handle when activity is recreated like on orientation Change
shouldDisplayHomeUp();
}

@Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}

public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
boolean canGoBack = getSupportFragmentManager().getBackStackEntryCount()>0;
getSupportActionBar().setDisplayHomeAsUpEnabled(canGoBack);
}

@Override
public boolean onSupportNavigateUp() {
//This method is called when the up button is pressed. Just the pop back stack.
getSupportFragmentManager().popBackStack();
return true;
}

How to handle up button inside fragment using Navigation Components

Finally, I found the solution.

First In the activity onCreate method I had to connect the navigation like I did:

val navController = this.findNavController(R.id.host_fragment)
NavigationUI.setupActionBarWithNavController(this, navController)

Then still in MainActivity override onSupportNavigateUp() :

override fun onSupportNavigateUp(): Boolean
{
val navController = this.findNavController(R.id.host_fragment)
return navController.navigateUp()
}

Then In the Fragment onCreateView I had to enable option menu:

setHasOptionsMenu(true)

then in the fragment I overridden onOptionsItemSelected :

override fun onOptionsItemSelected(item: MenuItem): Boolean
{
// handle the up button here
return NavigationUI.onNavDestinationSelected(item!!,
view!!.findNavController())
|| super.onOptionsItemSelected(item)
}

Note: I think if you have more than one option menu, then I think you have to do a when (item) statement to check what option has been chosen.

Also if you want to handle the device back button then you can do like this in your fragment onCreateViewMethod :

requireActivity().onBackPressedDispatcher.addCallback(this)
{
// handle back button

// change this line to whatever way you chose to navigate back
findNavController().navigate(NoteEditFragmentDirections.actionNoteEditFragmentToNoteListFragment())
}

How to do Up navigation using ActionBar's tabs and Fragment?

I didn't use the ActionBar in the end. Too many constraints (I had another issue and I wanted to add a View above the ActionBar, which seemed complicated).

It was much simpler doing my own layout from scratch.

Dynamic ActionBar title from a Fragment using AndroidX Navigation

As of 1.0.0-alpha08, you can have the NavigationUI bits dynamically set the title... if the dynamic bits are arguments on the navigation action.

So, for example, in your navigation graph, you could have something like this:

  <fragment
android:id="@+id/displayFragment"
android:name="com.commonsware.jetpack.sampler.nav.DisplayFragment"
android:label="Title: {title}" >
<argument
android:name="modelId"
app:argType="string" />
<argument
android:name="title"
app:argType="string" />
</fragment>

Here, the android:label attribute for our <fragment> has an argument name wrapped in braces ({title} in "Title: {title}". The app bar's title will then be set to the value of the label, with {title} replaced by the value of the title argument.

If you need something more elaborate than that — for example, you want to look up the model by ID and read a property from it — you will need to use more manual approaches, such as those outlined in other answers to this question.

Android Navigation Component ActionBar Up/Parent event handling

In fragment B in onCreateView enable options menu

setHasOptionsMenu(true)

And override onOptionsItemSelected

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) { // ActionBar back/parent button is pressed
// onBackPressed() // to return back to Fragment A
}
return true
}

Up Navigation (Action Bar's back arrow) is not working for fragments

Finally got the answer. In my scenario I'm disabling the drawer indicator by mActionBarDrawerToggle.setDrawerIndicatorEnabled(false); and due to this Navigation icon clicks got disabled. To enable this I've to add ToolbarNavigationClickListener to ActionBarDrawerToggle which will enable navigation icon clicks.

Below is the my working code :-

mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});

Refer this link for more clarification

Navigation component - Hide navigation up icon and fragments label from the custom toolbar

The toolbar.setupWithNavController() method calls through to the Toolbar methods of setNavigationIcon() and setTitle(). Those methods know nothing about your custom icon or title - they only update the built in navigation icon and title.

That means that toolbar.setupWithNavController() is not something you should ever be calling.

Instead, if you want your custom Toolbar layout to react to Navigation changes, you'll want to follow the guide for listening for navigation events and use an OnDestinationChangedListener to do whatever custom logic you want instead of calling toolbar.setupWithNavController():

val navController = findNavController(R.id.nav_host_fragment_auth)
val toolbar = findViewById<Toolbar>(R.id.toolbarAuth)
val mCustomView: View = layoutInflater.inflate(R.layout.custom_actionbar_auth, null)
val mButtonBack = mCustomView.findViewById<ImageView>(R.id.imageBack)
toolbar.addView(mCustomView)
navController.addOnDestinationChangedListener { _, destination, arguments ->
// Update the icon for your mButtonBack, etc.
}

If you just want the default behavior for the navigation up icon, you can call navigateUp() directly:

mButtonBack.setOnClickListener {
navController.navigateUp()
}


Related Topics



Leave a reply



Submit