从主要活动导航到片段

发布于 01-15 19:37 字数 288 浏览 3 评论 0原文

我使用导航图在片段之间导航。

现在,我在我的应用程序中实现了通知,因此,当我单击通知时,我将转到具有待处理意图的 MainActivity。

在 MainActivity 中,我验证用户是否已登录,并管理 putExtra 数据以了解导航到哪个片段。 问题是,activity_main.xml 似乎没有添加到导航图中,所以我没有 MainActivityDirections.actionMainActivityToFragment(argument)

我如何从活动导航到带有参数的一个片段?

Im using navgraph to navigate between fragments.

Now, i implemet notifications in my app, so, when i click on the notification im goingo to the MainActivity with pending intent.

In the MainActivity I verify if the user is loged in, and manage the putExtra data to know to which fragment navigate.
The problem is that the activity_main.xml doesn´t appear to add in the navgraph, so i don´t have MainActivityDirections.actionMainActivityToFragment(argument)

How can i navigate from the activity to one fragment with arguments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后来的我们2025-01-22 19:37:46

我实现了类似 @Grigoriym 的东西,但根据需要使用参数

MainActivity

val bundle = Bundle()
bundle.putParcelable("OrderNotification",navigator.order)
navController.navigate(R.id.orderDetailsFragment, bundle)

和 orderDetialsFragment

private val args: OrderDetailsFragmentArgs by navArgs()


val arg = arguments?.getParcelable<OrderUI>("OrderNotification")
    if(arg != null){
        orderArg = arg
    }else{
        orderArg = args.order
    }

I implemented something like @Grigoriym but with arguments as needed

MainActivity

val bundle = Bundle()
bundle.putParcelable("OrderNotification",navigator.order)
navController.navigate(R.id.orderDetailsFragment, bundle)

and in the orderDetialsFragment

private val args: OrderDetailsFragmentArgs by navArgs()


val arg = arguments?.getParcelable<OrderUI>("OrderNotification")
    if(arg != null){
        orderArg = arg
    }else{
        orderArg = args.order
    }
红焚2025-01-22 19:37:46

对于这种情况有一个解决方案:

val navController = findNavController(R.id.nav_host_test)
navController.navigate(R.id.action_global_test)

首先,您需要访问 navController,其中 id 是从 Activity 中的视图 id 中获取的。

输入图片description here

那么你需要创建一个全局动作,像这样(只注意动作):

<?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/mobile_navigation"
    app:startDestination="@+id/first_fragment">

    <action
        android:id="@+id/action_global_test"
        app:destination="@id/second_fragment"
        app:launchSingleTop="false"
        app:popUpTo="@+id/mobile_navigation"
        app:popUpToInclusive="true" />

    <fragment
        android:id="@+id/first_fragment"
        android:name="com.grappim.myapplication.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/second_fragment"
        android:name="com.grappim.myapplication.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

</navigation>

There is a solution for such a case:

val navController = findNavController(R.id.nav_host_test)
navController.navigate(R.id.action_global_test)

Firstly you need to need to access your navController where the id is taken from the view's id in your Activity.

enter image description here

then you need to create a global action, like this (pay attention only to the action):

<?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/mobile_navigation"
    app:startDestination="@+id/first_fragment">

    <action
        android:id="@+id/action_global_test"
        app:destination="@id/second_fragment"
        app:launchSingleTop="false"
        app:popUpTo="@+id/mobile_navigation"
        app:popUpToInclusive="true" />

    <fragment
        android:id="@+id/first_fragment"
        android:name="com.grappim.myapplication.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/second_fragment"
        android:name="com.grappim.myapplication.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

</navigation>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文