2

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.

g g
  • 97
  • 8
  • try adding this to your onCreateView and see if it works `if (getActivity() instanceof ActionBarActivity) { ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); }` – Javanshir Huseynli Jan 23 '21 at 15:06
  • 1
    It sounds like you're not following the [setting up the action bar documentation](https://developer.android.com/guide/navigation/navigation-ui#action_bar). Can you include your Activity code? – ianhanniballake Jan 23 '21 at 16:16
  • @ianhanniballake you are right. I didn't add toolbar as yet. I was curious why the back button was added in the default action bar but it doesn't work. – g g Jan 24 '21 at 01:53
  • Well, the docs I linked detail the two steps you need to do. Sounds like you only did one out of the two steps, but you'd need to include your Activity code if you want us to actually take a look at what you've done. – ianhanniballake Jan 24 '21 at 02:22
  • @ianhanniballake Yes, I didn't override onSupportNavigateUp(). I created a new project by choosing the bottoom navigation activity during the wizard. And created new fragment destination by reading the nav_graph part of the doc. Now my understand is, by default the button is working as "UP", although it looks more like a "BACK" button. That confused me a lot. Thanks a lot for your help. – g g Jan 24 '21 at 03:04
  • Thanks @JavanshirHuseynli. I didn't paste all my code. This should work between Activities. I have only one activity. – g g Jan 24 '21 at 03:07

2 Answers2

1

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

Sekiro
  • 1,537
  • 1
  • 7
  • 21
  • it works and thanks. As learnt from the doc my understaing BACK and UP are different. I know left facing caret is for UP. And left pointing arrow is for BACK. right? When did Android start using them mixedly. Or is there's place to address these icons? Thanks for you help. – g g Jan 24 '21 at 02:28
  • check the edited answer and see if it helps @gg – Sekiro Jan 24 '21 at 07:03
  • Thanks Sekiro. I remember I saw somewhere said "<-" is BACK and "<" is UP but can't find anymore. Maybe my mistake. Thanks a lot. – g g Jan 25 '21 at 03:19
1

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.

  • Thanks @Istiak, method one works without setDisplayHomeAsUpEnabled(true). And I changed finish() to onBackPressed() which suits my situaiton. I have only one Activity and others are all fragments. Method two, as Sekiro also suggested, works good as well. In my screenshot it's a BACK icon rather than UP icon, right? If yes why it does Android put the icon there but without enabling it. – g g Jan 24 '21 at 02:49
  • @gg I have edited my answer. I hope this edit will help you –  Jan 24 '21 at 05:03