Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

android - 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.

question from:https://stackoverflow.com/questions/65860631/why-the-back-button-doenst-work-by-default

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  • Back, the system button(left facing triangle), is used to navigate up the hierarchy and when in the home screen/fragment in your app and on pressing back, you will be navigated out of the app.

  • Up, the left-facing arrow in your appbar, is used to navigate up the hierarchy but it doesn't take you away from the app.

check out the docs for more info


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...