mar*_*987 3 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);
}
Run Code Online (Sandbox Code Playgroud)
我的主要活动确实使用了操作栏,其中显示了搜索栏和一些菜单项,我不知道这是否在某种程度上影响了它(已尝试将其删除而没有任何变化)
内容主体包含一个导航主机片段
<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"/>
Run Code Online (Sandbox Code Playgroud)
片段使用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>
Run Code Online (Sandbox Code Playgroud)
这是我举一个动作之一的例子,
Navigation.findNavController(requireActivity(),R.id.main_fragment)
.navigate(R.id.action_mainFragment_to_cardPreviewFragment);
Run Code Online (Sandbox Code Playgroud)
此操作导航到正确的位置,从标签中获取正确的标题,但不允许我使用箭头向后/向上导航,此处为完整项目
更新这是可重现的
我可以创建一个新项目,添加导航组件,并添加2个片段,添加导航图,并将navhost添加到内容主目录,最后我得到相同的行为(出现箭头,但不会弹出包括该图本身的任何内容),我正在使用“ androidx.navigation:navigation-fragment:2.1.0-alpha03“和” androidx.navigation:navigation-ui:2.1.0-alpha03“,我在这里制作了一个示例项目,并在问题跟踪器中引发了一个错误
谷歌更新已经回到我身边,说这是预期的行为,尽管我相当确定他被误解了,而且肯定没有运行我发送过来的例子
使用时setupWithActionBarWithNavController()
,您需要根据文档进行覆盖onSupportNavigateUp()
以触发操作栏中的“向上”图标:
@Override
public boolean onSupportNavigateUp() {
return Navigation.findNavController(this, R.id.main_fragment).navigateUp()
|| super.onSupportNavigateUp();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2041 次 |
最近记录: |