Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android navigation component up button, arrow displays but popTo not working

I'm using the jetpack navigation component with a very simple set up, one activity (MainActivity) and 4 fragments

MainFragment,

CardPreviewFragment,

createCardFragment,

and AddPredictionChipsFragment,

The NavigationUI component sets up my nav controller with my toolbar, I have actions set to each destination and popTo also set for each. I can successfully navigate to each of these fragments and the toolbars title changes correctly and home icon displays a back arrow correctly but pressing the arrow to use the popTo action does nothing (SIDENOTE if i set a pop enter and exit animation and use the back button to go back the animation is used). Any ideas welcome, below is the relevant code edited for brevity

MAIN ACTIVITY

public class MainActivity extends AppCompatActivity

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.content_main);
  setNavigation();
 }

private void setNavigation() {
 navController = Navigation.findNavController(this, R.id.main_fragment);
 NavigationUI.setupActionBarWithNavController(this, navController);
}

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
  return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
 Intent i;
 switch (item.getItemId()){
  case R.id.preferences :
    //code
    return true;
  case R.id.share :
    //code
  case R.id.create :
    //code
  case R.id.about :
    //code
    return true;
 }
 return super.onOptionsItemSelected(item);
}

My main activity does use the action bar, showing a search bar and some menu items, I don't know if this is somehow affecting it (have tried to remove them to no change)

content main holds a nav host fragment

<fragment
  android:id="@+id/main_fragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:name="androidx.navigation.fragment.NavHostFragment"
  app:defaultNavHost="true"
  app:navGraph="@navigation/nav_main"/>

Fragments use androidx : EditFragment extends Fragment and do not use menu options or anything similar they are all just standard fragments, here is my navigation graph

  <?xml version="1.0" encoding="utf-8"?>
    <navigation 
      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:id="@+id/nav_main"
      app:startDestination="@id/mainFragment">
      <fragment
        android:id="@+id/mainFragment"
        android:name="com.sealstudios.simpleaac.ui.MainFragment"
        android:label="Simple AAC"
        tools:layout="@layout/fragment_main">
        <action
          android:id="@+id/action_mainFragment_to_cardPreviewFragment"
          app:destination="@id/cardPreviewFragment"
          app:popUpTo="@+id/mainFragment"
          app:popUpToInclusive="false"/>
        <action
          android:id="@+id/action_mainFragment_to_createCardFragment"
          app:destination="@id/createCardFragment"
          app:popUpTo="@+id/mainFragment"
          app:popUpToInclusive="false"/>
      </fragment>
      <fragment
        android:id="@+id/cardPreviewFragment"
        android:name="com.sealstudios.simpleaac.ui.CardPreviewFragment"
        android:label="Preview Card"
        tools:layout="@layout/preview_card_fragment">
        <action
          android:id="@+id/action_cardPreviewFragment_to_createCardFragment"
          app:destination="@id/createCardFragment"
          app:popUpTo="@+id/mainFragment"
          app:popUpToInclusive="false"/>
      </fragment>
      <fragment
        android:id="@+id/addPredictionChipsFragment"
        android:name="com.sealstudios.simpleaac.ui.AddPredictionChipsFragment"
        android:label="Add Predictions"
        tools:layout="@layout/add_prediction_chips_fragment_layout"/>
      <fragment
        android:id="@+id/createCardFragment"
        android:name="com.sealstudios.simpleaac.ui.CreateCardFragment"
        android:label="Create / Edit"
        tools:layout="@layout/fragment_create_card">
        <action
          android:id="@+id/action_createCardFragment_to_addPredictionChipsFragment"
          app:destination="@id/addPredictionChipsFragment"
          app:popUpTo="@+id/createCardFragment"
          app:popUpToInclusive="false"/>
      </fragment>
    </navigation>

and here is an example of me calling one of the actions,

  Navigation.findNavController(requireActivity(),R.id.main_fragment)
  .navigate(R.id.action_mainFragment_to_cardPreviewFragment);

this action navigates to the correct place getting the correct title from the label but does not allow me to navigate back/up using the arrow, full project is here

UPDATE This is reproducible

I can create a new project add the navigation component and 2 fragments add the nav graph and add the navhost to content main, and i end up with the same behaviour (arrow appears but wont popTo anything including the graph itself) I'm using "androidx.navigation:navigation-fragment:2.1.0-alpha03" and "androidx.navigation:navigation-ui:2.1.0-alpha03" and I've made a sample project here and raised a bug in the issue tracker

UPDATE google have come back to me and said this is the expected behaviour although I'm fairly sure he's misunderstood and definitely hasn't run the example I sent over

like image 528
martinseal1987 Avatar asked May 17 '19 21:05

martinseal1987


Video Answer


1 Answers

When using setupActionBarWithNavController(), you need to override onSupportNavigateUp() to trigger the Up icon in the Action Bar as per the documentation:

@Override
public boolean onSupportNavigateUp() {
    return Navigation.findNavController(this, R.id.main_fragment).navigateUp()
            || super.onSupportNavigateUp();
}
like image 149
ianhanniballake Avatar answered Oct 18 '22 21:10

ianhanniballake