g g
g g

Reputation: 107

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.

Upvotes: 2

Views: 3633

Answers (3)

cumpatomas
cumpatomas

Reputation: 61

in Kotlin:

setSupportActionBar(yourToolBarId)

supportActionBar?.setDisplayHomeAsUpEnabled(true)

override fun onSupportNavigateUp(): Boolean {

    onBackPressedDispatcher.onBackPressed()
    return true
}

Upvotes: 1

Dhanuesh
Dhanuesh

Reputation: 1596

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

Upvotes: 2

user13146129
user13146129

Reputation:

To enable the back button

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

To work

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { 
    switch (item.getItemId()) { 
        case android.R.id.home: 
            this.finish(); 
            return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

Alternative way to work the button

public boolean onSupportNavigateUp(){
    onBackPressed();
    return true;
}

After googling little bit I found an answer for you.

As you said you have back button in actionBar in Fragment. But, you also said it isn't working. So, why android studio implement it?

Here is the answer for you.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case android.R.id.home:
    switch(currentFragment){
    case FRAGMENT1:
        FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction
    .replace(R.id.fragment_container, fragment2);
    transaction.commit();
    currentFragment = FRAGMENT_2;
    return true;

default:
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction
    .replace(R.id.fragment_container, fragment1);
    transaction.commit();
    currentFragment = FRAGMENT_1;
    return true;
}
}

I found another useful link for you but, which may not be similar to your question.

Upvotes: 2

Related Questions