android导航组件向上按钮,箭头显示但popTo不起作用 [英] android navigation component up button, arrow displays but popTo not working

查看:36
本文介绍了android导航组件向上按钮,箭头显示但popTo不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

主片段,

卡片预览片段,

createCardFragment,

createCardFragment,

和 AddPredictionChipsFragment,

and AddPredictionChipsFragment,

NavigationUI 组件使用我的工具栏设置我的导航控制器,我为每个目的地设置了操作,还为每个目的地设置了 popTo.我可以成功导航到这些片段中的每一个,并且工具栏标题正确更改并且主页图标正确显示后退箭头但按下箭头以使用 popTo 操作没有任何作用(旁注,如果我设置了弹出输入和退出动画并使用后退按钮回去使用动画).欢迎任何想法,以下是为简洁起见编辑的相关代码

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

主要活动

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 包含一个导航主机片段

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"/>

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

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

更新这是可重现的

我可以创建一个新项目添加导航组件和 2 个片段添加导航图并将导航主机添加到内容主,我最终得到相同的行为(出现箭头但不会弹出任何内容,包括图形本身)我'我使用androidx.navigation:navigation-fragment:2.1.0-alpha03"和androidx.navigation:navigation-ui:2.1.0-alpha03",我做了一个示例项目 这里 并在问题跟踪器中提出了一个错误

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

推荐答案

使用 setupActionBarWithNavController() 时,需要重写 onSupportNavigateUp() 来触发 Up 图标根据文档:

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

这篇关于android导航组件向上按钮,箭头显示但popTo不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆