安卓导航组件的返回按钮,箭头显示但popTo功能无效。

20
我正在使用Jetpack Navigation组件,只有一个活动(MainActivity)和四个片段:MainFragment,CardPreviewFragment,createCardFragment和AddPredictionChipsFragment。
NavigationUI组件使用我的工具栏设置了我的导航控制器,我为每个目标设置了操作,并且也为每个目标设置了popTo。 我可以成功地导航到这些片段中的每一个,工具栏的标题正确更改,主页图标正确显示返回箭头,但按下箭头以使用popTo操作不起作用(如果我设置了弹出进入和退出动画并使用返回按钮返回,则会使用该动画)。 任何想法都受欢迎,以下是为简洁起见编辑过的相关代码。 主要活动:
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);
}

我的主要活动确实使用了操作栏,显示搜索栏和一些菜单项,我不知道这是否会对其产生影响(尝试删除它们但并没有改变)

content main 包含一个 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"/>

片段使用androidx:EditFragment扩展了Fragment,并不使用菜单选项或类似的任何内容,它们都只是标准片段,这是我的导航图

  <?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>

这里是我调用其中一个动作的示例,

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

这个操作通过从标签获取正确的标题来导航到正确的位置,但是不能使用箭头返回/向上导航,完整项目在这里

更新:这是可重现的

我可以创建一个新项目添加导航组件和2个fragment,添加nav图并将navhost添加到content main中,最终出现相同的行为(箭头出现但不会导航到任何东西,包括图形本身)。我正在使用 "androidx.navigation:navigation-fragment:2.1.0-alpha03" 和 "androidx.navigation:navigation-ui:2.1.0-alpha03",我已经制作了一个示例项目here 并在问题跟踪器中提出了一个错误

更新:谷歌回复我说这是预期的行为,尽管我很确定他误解了,并且肯定没有运行我发送的示例

2个回答

41

在使用setupActionBarWithNavController()时,需要覆盖onSupportNavigateUp()方法,以触发操作栏中的向上图标,如文档所述:

@Override
public boolean onSupportNavigateUp() {
    return Navigation.findNavController(this, R.id.main_fragment).navigateUp()
            || super.onSupportNavigateUp();
}

昨晚在谷歌开发者的提示下才发现了这个问题,之前没注意到,非常感谢。 - martinseal1987
我实验性地发现,即使您在代码中使用了onSupportNavigateUp()方法,如果您的MainActivity中没有使用DrawerLayout,则导航向上箭头按钮不会显示。 - Ercan
1
根据文档,当您位于顶级目的地(默认情况下为图形的起始目的地)时,不会出现向上箭头。 - ianhanniballake
有时候 onNavigateUp() 不起作用,但 onSupportNavigateUp() 一定会。 - Willey Hute

0
此外,如果您想要为片段启用返回按钮,您可以子类化工具栏并进行设置。
override fun onAttachedToWindow() {
    super.onAttachedToWindow()
    val navController = (context as AppCompatActivity).supportFragmentManager.findFragmentById(R.id.nav_host)!!.findNavController()
    setupWithNavController(navController)
}

在其中编写类似上面的内容,假设您正在使用AppcompatActivity,它具有您的NavHostFragment,因此它具有导航控制器,因此您可以在此处设置它与navcontroller而不是按照文档中所述在每个片段中执行此操作。

子类化工具栏具有更多优点,例如您可以为所有片段设置返回图标,只需在一个地方进行设置,还可以更改工具栏标题的字体和位置,这是工具栏默认不支持的。

它会像这样做一些事情

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
    super.onLayout(changed, l, t, r, b)
    children.forEach {
        if(it is TextView && !titleSet){
            titleSet  = true
            it.x -= 50
            it.typeface = ResourcesCompat.getFont(context, R.font.poppins_bold)
            return@forEach
        }
    }
}

上述解决方案在许多应用程序中没有经过测试,可能不足以满足您的使用情况,但它对我起作用了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接