代码之家  ›  专栏  ›  技术社区  ›  Ahmed S. Durrani

不使用单一活动的导航体系结构

  •  0
  • Ahmed S. Durrani  · 技术社区  · 7 年前

    Navigation Architecture 框架来显示一些片段。不幸的是,每个片段都在创建一个新的活动。 Activity 具有 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) 但他们没有太大的帮助,因为使用他们不会显示任何碎片后,第一次显示。

    <?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"
            android:id="@+id/notification_nav_graph"
            app:startDestination="@id/openingNotificationFragment">
            <fragment
                android:id="@+id/standByScreenFragment"
                android:name="com.emaz.android.ui.notification.standby.StandByScreenFragment"
                android:label="StandByScreenFragment" />
            <fragment
                android:id="@+id/welcomeNotificationFragment"
                android:name="com.emaz.android.ui.notification.welcome.WelcomeNotificationFragment"
                android:label="WelcomeNotificationFragment" />
            <fragment
                android:id="@+id/openingNotificationFragment"
                android:name="com.emaz.android.ui.notification.ingress..OpeningNotificationFragment"
                android:label="OpeningFragment" />
            <fragment
                android:id="@+id/closingNotificationFragment"
                android:name="com.emaz.android.ui.notification.ingress..ClosingNotificationFragment"
                android:label="ClosingNotificationFragment" />
        </navigation>
    

    家庭活动 打电话 NotificationActivity 作为

    val intent = Intent(this, NotificationActivity::class.java)
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                    startActivity(intent)
    

    notification_container.visibility = View.GONE
                fragment_layout.visibility = View.VISIBLE
                val navController = Navigation.findNavController(this, nav_host_fragment.id)
    
                navController.popBackStack()
                Log.e("nav", "${navController.currentDestination} and $resId < ResourceId")
    
                navController.navigate(resId, null)
    

    resId 请帮忙。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ahmed S. Durrani    7 年前

    我解决了创建多个活动的问题。对于其他面临同样问题的人。如果您试图使用导航架构导航到片段,请确保应用程序不在后台。

    保持应用程序处于活动状态不会创建活动的新实例,应用程序将按预期工作。

    推荐文章