COYG
COYG

Reputation: 1598

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

I am using the navigation component architecture. I have a menu item in the action bar where the user can click to go to the settings. I am using a one activity to many fragments approach. So this action bar, with the menu item is across all of my fragments.

Lets say I have 3 fragments. and fragment A is the main fragment. While I am in the settings preferences the 'UP' button always takes me back to fragment A. Even if I called it from frag B or C.

Here's what I mean by the up button (as it is called here):

enter image description here

And here is my actual NavGraph

enter image description here

From this you can see that Frag A is linked by an action to settings. I did this because this is the home fragment where the host activity for the fragments begins. It doesn't make a difference anyway I deleted this action and it still behaved in the same way.

Here is my onOptionsItemSelected

//Preform action when selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    return NavigationUI.onNavDestinationSelected(item, navController)
            || super.onOptionsItemSelected(item);
}

So, how do I, if going to settings from Fragment C, get back to fragment C. Instead of Fragment A?

Upvotes: 1

Views: 201

Answers (2)

COYG
COYG

Reputation: 1598

NavigationUI contains a onNavDestinationSelected method for handling menu items. This method pops the whole back-stack by default. The fix is to add android:menuCategory="secondary" to your xml for the items in your menu as is mentioned here:

enter image description here

Upvotes: 1

Zain
Zain

Reputation: 40878

So, how do I, if going to settings from Fragment C, get back to fragment C. Instead of Fragment A?

I am assuming that you already added a navigation action from Fragment C to Settings fragment so that you can navigate from Fragment C to the SettingsFragment.

So, here is a demo sample of this action at the navigation graph:

<fragment
    android:id="@+id/cFragment"
    android:name="...."
    android:label="fragment_c"
    tools:layout="@layout/fragment_c" >

    ....

    <action
        android:id="@+id/action_cFragment_to_settingsFragment"
        app:destination="@id/settingsFragment"
        app:popUpTo="@id/cFragment" />
</fragment>

Now you want to hit the up/home button on the SettingsFragment to back to Fragment C (or whatever fragment invokes the SettingsFragment.

You can use the back stack to do that; where the top fragment at the back stack is now FragmentC which is determined by the action action_cFragment_to_settingsFragment.

You can use onBackPressed() of the sole activity to achieve this, so that now the SettingFragmentcan back to whatever next fragment at the back stack.

To do that: Create the SettingsFragment like below:

public class SettingsFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_settings, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setHasOptionsMenu(true);
        ((AppCompatActivity) requireActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            requireActivity().onBackPressed(); // Back to the next fragment at the back stack
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

Note: if you see the home button on other fragments you can remove it by:

setHasOptionsMenu(false);
((AppCompatActivity) requireActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);

Upvotes: 0

Related Questions