6

在我的应用程序中,用户可以选择一个类别,然后在该类别中选择一个项目以最终查看项目详细信息。标准/正向流程是:

SelectCategoryFragment-> SelectItemFragment->ViewItemDetailsFragment

在选择一个类别时,selectedCatId通过Bundlefrom SelectCategoryFragmentto传递SelectItemFragment

    NavController navController = Navigation.findNavController(v);
    Bundle args = new Bundle();
    args.putLong(SelectItemFragment.ARG_CATEGORY_ID, selectedCatId);
    navController.navigate(R.id.action_nav_categories_to_items, args);

SelectItemFragment然后将使用该getArguments().getLong(ARG_CATEGORY_ID)值来查询和显示所选类别中的适当项目。

这很好用。但是我现在正在尝试在用户点击通知时实现深度链接,将他们直接跳转到ViewItemDetailsFragment可以将他们带到SelectItemFragment, 然后SelectCategoryFragment.

我的问题是,如上所述,SelectItemFragment取决于ARG_CATEGORY_ID传递给它的参数以检索/显示其数据。我已经阅读了深度链接嵌套导航图,但真的不知道如何通过ARG_CATEGORY_ID深度链接/后台堆栈传递。

ViewItemDetailsFragment有没有一种整洁的方法可以SelectItemFragment在用户按下时传递数据?

4

1 回答 1

4

TLDR:问题可以使用嵌套图来解决。有关详细信息,请参阅本文

更长的答案

让我们定义简单的片段FragmentRedFragmentGreenFragmentBlue为每个简单的布局添加相应的背景颜色:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_dark">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

并像这样声明片段类:

class FragmentRed : Fragment() {

    private val args: FragmentRedArgs by navArgs()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.red, container, false)
        view.findViewById<TextView>(R.id.textView).text = args.foo.toString()
        return view
    }
}

FragmentGreen并被FragmentBlue复制粘贴,但用相应的颜色文本替换所有颜色文本,即FragmentRedArgs-> FragmentBlueArgsR.layout.red-> R.layout.blue

让我们这样声明主要活动布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:id="@+id/content"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/main_graph" />
</FrameLayout>

在哪里main_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"
    android:id="@+id/main_graph"
    app:startDestination="@id/fragment_red">

    <fragment
        android:id="@+id/fragment_red"
        android:name="com.playground.FragmentRed">
        <argument
            android:name="foo"
            android:defaultValue="0"
            app:argType="integer" />
        <action
            android:id="@+id/action_fragment_red_to_fragment_green"
            app:destination="@id/fragment_green" />
    </fragment>

    <navigation
        android:id="@+id/secondLevel"
        app:startDestination="@id/fragment_green">

        <fragment
            android:id="@+id/fragment_green"
            android:name="com.playground.FragmentGreen">
            <argument
                android:name="bar"
                android:defaultValue="0"
                app:argType="integer" />
            <action
                android:id="@+id/action_fragment_green_to_fragment_blue"
                app:destination="@id/fragment_blue" />
        </fragment>

        <fragment
            android:id="@+id/fragment_blue"
            android:name="com.playground.FragmentBlue">
            <argument
                android:name="zar"
                android:defaultValue="0"
                app:argType="integer" />
        </fragment>
    </navigation>

</navigation>

现在MainActivity让我们在内部生成一个新通知,为每个片段传递参数:“foo”(红色)- 1、“bar”(绿色)- 2、“zar”(蓝色)- 3。

我们的期望是点击通知以打开带有文本 3 的蓝屏,在返回点击时会看到带有 2 的绿屏,再次点击应该会在屏幕上显示带有 1 的红屏:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navController = findNavController(R.id.nav_host_fragment)
        val pendingIntent = navController.createDeepLink()
            .setGraph(R.navigation.main_graph)
            .setDestination(R.id.fragment_blue)
            .setArguments(bundleOf("foo" to 1, "bar" to 2, "zar" to 3))
            .createPendingIntent()

        createNotificationChannel() // outside of the scope of this answer
        val builder = NotificationCompat.Builder(this, "my_channel")
            .setContentTitle("title")
            .setContentText("content text")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.android)
            .setAutoCancel(true)
            .setChannelId("channelId")

        with(NotificationManagerCompat.from(this)) {
            notify(100, builder.build())
        }
    }

}

这是设备上的实际行为:

在此处输入图像描述

于 2020-06-19T18:27:02.113 回答