在Android导航组件中使用navigateUp传递数据/捆绑包

在Android导航组件中使用navigateUp传递数据/捆绑包,android,android-fragments,android-architecture-navigation,android-safe-args,Android,Android Fragments,Android Architecture Navigation,Android Safe Args,我想在发生反压/手动反压时获得数据。我正在使用navigateUp()返回。如何将数据传递到上一个片段navigateUp()没有任何工具将数据传递到上一个片段。甚至我也没有找到解决方法。它正在向前传递数据。我想要在向后的框架B->框架A 我的代码返回到上一个片段 Navigation.findNavController(view).navigateUp() 我的问题是,如何获得前面片段中的数据。我可以使用从Frag B导航到Frag A,根据,您可以使用common for Fragmen

我想在发生反压/手动反压时获得数据。我正在使用
navigateUp()
返回。如何将数据传递到上一个片段
navigateUp()
没有任何工具将数据传递到上一个片段。甚至我也没有找到解决方法。它正在向前传递数据。我想要在向后的框架B->框架A

我的代码返回到上一个片段

Navigation.findNavController(view).navigateUp()

我的问题是,如何获得前面片段中的数据。我可以使用

从Frag B导航到Frag A,根据,您可以使用common for Fragment,其中您希望使用其活动范围共享数据

以下是步骤:

  • 创建将保留数据的视图模型:
  • 我希望答案有帮助。

    你们可以使用图书馆。基本上,它是
    startActivityForResult
    ,但对于导航组件中的片段而言。

    您只需调用

    findNavController().导航(R.id.fragment1,args)

    args是您的包。
    在fragment1中,从参数获取数据

    您应该使用静态变量/伴随对象,因为它比shared viewmodel好,因为它不是简单/好的体系结构。由于这并不简单,我认为这是最好的办法

    将EUP从FragmentB导航到FragmentA

    片段b:

    isBackpressed = true
    findNavController().navigateUp() 
    
    片段a:

    onViewCreated() {
        // todo
        if(isBackpressed) {
             isBackpressed = false
             // do whatever you want
        }
    }
    

    要在从一个目的地导航到另一个目的地时弹出目的地,请向关联的
    元素添加
    app:poputo
    属性。 要使用参数从Farming2导航到Fragment1,请在导航图中指定调用者片段的
    操作
    ,以及目标片段的
    参数

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.myapplication.Fragment2"
        android:label="fragment_2"
        tools:layout="@layout/fragment_2">
    
        <action
            android:id="@+id/action_2_to_1"
            app:destination="@id/fragment1"
            app:popUpTo="@+id/fragment1"/>
    </fragment>
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.myapplication.Fragment1"
        android:label="fragment_1"
        tools:layout="@layout/fragment_1">
    
            <argument
                android:name="someArgument"
                app:argType="string"
                app:nullable="false"
                android:defaultValue="Hello Word"/>
    </fragment>
    

    请使用官方的androidx组件。方法:


    干杯;)

    第二种解决方案是单例java
    companion object
    在kotlinDid你找到什么解决方案了吗?我已经使用了
    companion object
    我希望Android团队很快会为这个简单的操作提供简单方便的解决方案。嗨!你能将你的解决方案发布到@BhaveshHirpara吗?这是在创建新的片段还是在向上导航?我想back@BhaveshHirpara请看我在这里的回答:navigateUp()和navigate执行两个不同的操作,这不是一个好的回答这将再次实例化fragment1在Fragment之间共享ViewModel将增加源代码的复杂性。@Natig如果我使用SharedViewModel,那么我将在一个片段中有两个viewModels?在FragmentA中,我将有FragmentAViewModel和SharedViewModel,在FragmentB中,我将有FragmentBViewModel和SharedViewModel?那么共享视图模型只是为了将数据传递回前一个片段?@Natig还是我只需要为我的两个片段制作一个视图模型(SharedViewModel)?我很困惑。对不起,我是一个初学者,这就是为什么他们支持共享视图模型
    isBackpressed = true
    findNavController().navigateUp() 
    
    onViewCreated() {
        // todo
        if(isBackpressed) {
             isBackpressed = false
             // do whatever you want
        }
    }
    
    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.myapplication.Fragment2"
        android:label="fragment_2"
        tools:layout="@layout/fragment_2">
    
        <action
            android:id="@+id/action_2_to_1"
            app:destination="@id/fragment1"
            app:popUpTo="@+id/fragment1"/>
    </fragment>
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.myapplication.Fragment1"
        android:label="fragment_1"
        tools:layout="@layout/fragment_1">
    
            <argument
                android:name="someArgument"
                app:argType="string"
                app:nullable="false"
                android:defaultValue="Hello Word"/>
    </fragment>
    
       val action = Fragment2Directions.action2To1("MY_STRING_ARGUMENT")
            findNavController().navigate(action)
    
    implementation "androidx.fragment:fragment-ktx:1.3.3"