Actionbar图标单击和向上箭头单击问题

问题描述 投票:0回答:1

我正在将navgraph与nav组件一起使用。这是我的navgraph的设置方式:

主要活动-> CategoriesFragment(Home Fragment)-> CategoryFragment-> PomoClockFragment

我执行以下导航CategoriesFragment-> CategoryFragment-> PomoClockFragment。

此时,反复按电话的物理后退按钮或Actionbar的向上按钮可与片段返回堆栈一起正常使用。但是,如果我按Actionbar中的图标显示另一个片段,然后单击“向上”箭头,它会直接返回到原始片段(CategoriesFragment),而不是返回到堆栈上的最新片段。我该如何解决?我还尝试了其他方法,这些方法在下面的onSupportNavigateUp()方法的“活动代码”中显示为注释。

主要活动

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

Navgraph

<?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
1个回答
2
投票

ActionBar中向上箭头的默认操作是NavigateUp,这将启动父活动,而不是完成当前活动。

onOptionItemSelected并调用活动item.itemId == android.R.id.home方法时(您需要覆盖fragmentmanager.popStackBack()(加上处理堆栈大小为0并完成该操作))调用onBackPressed()

© www.soinside.com 2019 - 2024. All rights reserved.