android导航组件上移按钮,显示箭头,但popTo不起作用

时间:2019-05-17 21:28:36

标签: android android-fragments android-actionbar android-jetpack-navigation

我正在使用一个非常简单的设置,一个活动(MainActivity)和4个片段的jetpack导航组件

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);
}

片段使用androidx:EditFragment扩展了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"/>

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

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

此操作导航到正确的位置,从标签中获取正确的标题,但不允许我使用箭头向后/向上导航,整个项目为here

更新这是可重复的

我可以创建一个新项目,添加导航组件,并添加2个片段,添加导航图,并将navhost添加到内容主目录,然后我得到相同的行为(出现箭头,但不会弹出包括该图本身的任何内容)我m使用“ androidx.navigation:navigation-fragment:2.1.0-alpha03”和“ androidx.navigation:navigation-ui:2.1.0-alpha03”,我制作了一个示例项目here,并提出了一个错误在问题跟踪器中

更新谷歌回到我身边,说这是预期的行为,尽管我相当确定他被误解了,而且肯定没有运行我发送过来的示例

2 个答案:

答案 0 :(得分:0)

使用setupWithActionBarWithNavController()时,您需要根据the documentation覆盖onSupportNavigateUp()来触发操作栏中的向上图标:

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

答案 1 :(得分: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
        }
    }
}

上述解决方案未在许多应用中进行测试,可能不足以满足您的用例,但对我有用。

相关问题