androidandroid-jetpackandroid-architecture-navigation

why the back button doens't work by default


I'm learning the nav_graph feature and created one new fragment(AddNoteFragment). After editing in the navigation design UI, yes I can navigated to AddNoteFragment from NoteFragment, and there's a back icon(left-pointing arrow) on the topleft corner. I assume it's handled by the framework itself and it should be able to navigate me back if I click the button. See below screenshot. enter image description here But actually it has no action as I clicked. I searched for similar questions and tried override "onOptionsItemSelected" but no luck. One thing I haven't try yet is to add new tool bar in the fragment, because I think I don't have to. There should be a way to make the current button work. Shouldn't it have a default behavior defined? Else what's meaning of displaying the icon? This is a common bahavior and requirment.

Related code for your reference. frangment "navigation_note" is one of the three bottom navigation tab fragments. You can see the I added the action for navigating to "addNoteFragment".

<fragment
    android:id="@+id/navigation_note"
    android:name="com.guo.ultrasecretary.ui.note.NoteFragment"
    android:label="@string/title_note"
    tools:layout="@layout/fragment_note" >
    <action
        android:id="@+id/action_navigation_note_to_addNoteFragment"
        app:destination="@id/addNoteFragment" />
</fragment>

java code for addNoteFragment:

public class AddNoteFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View addNoteView = inflater.inflate(R.layout.fragment_note_add, container, false);
    return addNoteView;
}

//tried but not working
/*    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                getActivity().onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }*/
}

layout of the fragment:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/textInputEditText2"
        android:layout_width="293dp"
        android:layout_height="94dp"
        android:hint="Input note here"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="394dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

I'm using Android Studio 4.1 and running the code on AVD Nexus 6. Please help to correct me.


Solution

  • Clicking the up button triggers the onSupportNavigateUp and as mentioned in the official docs,

    If a parent was specified in the manifest for this activity or an activity-alias to it, default Up navigation will be handled automatically.

    To implement the expected behaviour override onSupportNavigateUp instead of onOptionsItemSelected

        override fun onSupportNavigateUp(): Boolean {
            return navController.navigateUp()
        }
    

    Edit:

    The Up and Back buttons are used to navigate up the hierarchy and the difference between them is

    check out the docs for more info