【发布时间】:2020-05-19 06:51:20
【问题描述】:
我正在使用带有 navgraph 的 nav 组件。这就是我的导航图的设置方式:
主要活动 -> CategoriesFragment (Home Fragment) -> CategoryFragment -> PomoClockFragment
我执行以下导航CategoriesFragment -> CategoryFragment -> PomoClockFragment。
此时,重复按手机的物理后退按钮或Actionbar 的向上按钮可以正常使用片段返回堆栈。但是如果我按下Actionbar 中的一个图标来显示另一个片段,然后单击向上箭头,它会直接返回到主片段(CategoriesFragment),而不是返回到后堆栈上的最新片段。我该如何解决?我还尝试了其他方法,这些方法在下面的 onSupportNavigateUp() 方法中的活动代码中显示为 cmets。
主要活动
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
Log.i("Lifecycle-Activity", "OnCreate() called")
navController = Navigation.findNavController(this, R.id.navHostFragment)
NavigationUI.setupActionBarWithNavController(this, navController)
appBarConfiguration = AppBarConfiguration.Builder(navController.graph)
.build()
}
override fun onSupportNavigateUp(): Boolean {
super.onSupportNavigateUp()
return NavigationUI.navigateUp(navController, appBarConfiguration)
//*** I also tried these, but does not solve the problem I am having ***
//return Navigation.findNavController(this, R.id.navHostFragment).navigateUp() || super.onSupportNavigateUp()
//supportFragmentManager.popBackStack()
//return navController.navigateUp()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
Log.i("Lifecycle-Activity", "OnCreateOptionsMenu() called")
menuInflater.inflate(R.menu.main_menu, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
Log.i("Lifecycle-Activity", "OnOptionsItemSelected() called")
return NavigationUI.onNavDestinationSelected(item, navController) || super.onOptionsItemSelected(item)
}
导航图
<?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_graph_main"
app:startDestination="@id/categoriesFragment">
<fragment
android:id="@+id/clockFragment"
android:name="com.example.pomoplay.ui.main.ClockFragment"
android:label="Pomo Clock"
tools:layout="@layout/fragment_clock" />
<fragment
android:id="@+id/categoryFragment"
android:name="com.example.pomoplay.ui.main.CategoryFragment"
android:label="Category"
tools:layout="@layout/fragment_category">
<action
android:id="@+id/action_categoryFragment_to_clockFragment"
app:destination="@id/clockFragment" />
<argument
android:name="category"
app:argType="com.example.pomoplay.Category"
app:nullable="true"
android:defaultValue="@null" />
<action
android:id="@+id/action_categoryFragment_to_newTaskDialogFragment"
app:destination="@id/newTaskDialogFragment" />
<argument
android:name="pomotask"
app:argType="com.example.pomoplay.PomoTask"
app:nullable="true"
android:defaultValue="@null" />
<argument
android:name="fromNewTaskDialog"
app:argType="boolean"
android:defaultValue="false" />
<argument
android:name="fromCategoriesFragmentTitle"
app:argType="boolean"
android:defaultValue="false" />
</fragment>
<fragment
android:id="@+id/categoriesFragment"
android:name="com.example.pomoplay.ui.main.CategoriesFragment"
android:label="Categories"
tools:layout="@layout/fragment_categories">
<action
android:id="@+id/action_categoriesFragment_to_newCategoryDialogFragment"
app:destination="@id/newCategoryDialogFragment" />
<argument
android:name="category"
app:argType="com.example.pomoplay.Category"
app:nullable="true" />
<action
android:id="@+id/action_categoriesFragment_to_categoryFragment"
app:destination="@id/categoryFragment" />
<argument
android:name="fromNewCategoryDialog"
app:argType="boolean"
android:defaultValue="false" />
</fragment>
<dialog
android:id="@+id/newCategoryDialogFragment"
android:name="com.example.pomoplay.ui.main.NewCategoryDialogFragment"
tools:layout="@layout/fragment_new_category_dialog">
<action
android:id="@+id/action_newCategoryDialogFragment_to_categoriesFragment"
app:destination="@id/categoriesFragment" />
</dialog>
<dialog
android:id="@+id/newTaskDialogFragment"
android:name="com.example.pomoplay.ui.main.NewTaskDialogFragment"
android:label="fragment_new_task_dialog"
tools:layout="@layout/fragment_new_task_dialog" >
<action
android:id="@+id/action_newTaskDialogFragment_to_categoryFragment"
app:destination="@id/categoryFragment" />
</dialog>
</navigation>
【问题讨论】:
标签: android android-fragments android-actionbar android-navigation