Pass data back to previous fragment using Android Navigation

前端 未结 4 555
天命终不由人
天命终不由人 2021-01-07 21:54

I\'ve started using Android Architecture Components (Navigation and Safe Args, View Models) along with Koin library.

Currently, I\'ve got a problem with passing argu

相关标签:
4条回答
  • 2021-01-07 22:15

    1) Pass string from Fragment A to Fragment B with action_A_to_B and SafeArgs.

    2) popBackStack to remove Fragment B.

    navController.popBackStack(R.id.AFragment, false);
    

    or

    navController.popBackStack();
    

    3) Then pass modified data from B to A with action_B_to_A.

    EDIT.

    Here you have some another solution

    0 讨论(0)
  • 2021-01-07 22:33

    Currently, I've got a problem with passing arguments between two fragments - I need to pass a string value from fragment A to fragment B, modify this value in fragment B and pass it back to fragment A.

    The theoretical solution really is to have the two fragments in a shared <navigation tag, then scope the ViewModel to the ID of the navigation tag, this way you now share the ViewModel between the two screens.

    To make this reliable, it's best to use the NavBackStackEntry of the Navigation tag as both a ViewModelStoreOwner and SavedStateRegistryOwner, and create an AbstractSavedStateViewModelFactory that will create the ViewModel using the ViewModelProvider, while also giving you a SavedStateHandle.

    You can communicate the results from FragmentB to FragmentA using this SavedStateHandle, associated with the shared ViewModel (scoped to the shared NavGraph).

    0 讨论(0)
  • 2021-01-07 22:37

    Android just released a solution for this; Passing data between Destinations (Navigation 2.3.0-alpha02), basically, in fragment A you observe changes in a variable and in fragment B you change that value before executing popBackStack().

    Fragment A:

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val navController = findNavController();
    // We use a String here, but any type that can be put in a Bundle is supported
    navController.currentBackStackEntry?.savedStateHandle?.getLiveData("key")?.observe(
        viewLifecycleOwner) { result ->
        // Do something with the result.
      }
    }
    

    Fragment B:

    navController.previousBackStackEntry?.savedStateHandle?.set("key", result)
    navController.popBackStack()
    
    0 讨论(0)
  • 2021-01-07 22:38

    You can try this solution

    <fragment
        android:id="@+id/a"
        android:name="...">
    
        <argument
            android:name="text"
            app:argType="string" />
    
        <action
            android:id="@+id/navigate_to_b"
            app:destination="@id/b" />
    
    </fragment>
    
    <fragment
        android:id="@+id/b"
        android:name="...">
    
        <argument
            android:name="text"
            app:argType="string" />
    
        <action
            android:id="@+id/return_to_a_with_arguments"
            app:destination="@id/a"
            app:launchSingleTop="true"
            app:popUpTo="@id/b"
            app:popUpToInclusive="true" />
    
    </fragment>
    
    

    and navigation fragment

    NavHostFragment.findNavController(this).navigate(BFragmentDirections.returnToAWithArguments(text))
    

    ianhanniballake`s comment has helped me solve a similar problem

    0 讨论(0)
提交回复
热议问题