Skip to content Skip to sidebar Skip to footer

Pass Data/bundle Using Navigateup In Android Navigation Component

I found the question but does not have solution in code I want to have data when backpress/manual back happens. I am using navigateUp() to go back. How can I pass data to previous

Solution 1:

According to developer.android.com, you can use common for fragments where you want to share data ViewModel using their activity scope.

Here are steps:

  1. Create view model which will keep the data:
classSharedViewModel : ViewModel() {
    val dataToShare = MutableLiveData<String>()

    funupdateData(data: String) {
        dataToShare.value = data
    }
}
  1. Observe data changes in Fragment1:
classFragment1 : Fragment() {

    privatelateinitvar viewModel: SharedViewModel

    overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProviders.of(activity!!).get(SharedViewModel::class.java)
        viewModel.dataToShare.observe(this, Observer<String> { dataFromFragment2 ->
            // do something with data
        })
    }
}
  1. Update data in Fragment2 and if you're handling navigation properly, now, you should be able to receive data changes on Fragment1:
classFragment2 : Fragment() {

    privatelateinitvar viewModel: SharedViewModel

    overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProviders.of(activity!!).get(SharedViewModel::class.java)

        updateDataButton.setOnClickListener { v ->
            viewModel.updateData("New data for fragment1")
        }
    }
}

I hope answer helps.

Solution 2:

You can use NavigationResult library. Basically it's startActivityForResult but for Fragments in Navigation component.

Solution 3:

Please use the OFFICIAL androidx's components. setFragmentResultListener() and setFragmentResult() methods:

implementation "androidx.fragment:fragment-ktx:1.3.5"

Cheers ;)

Solution 4:

You should use static variables/companion objects, because it is better than shared viewmodel as it is not simple/nice architecture. As it it not straightforward, I think it is the best way.

To navigateUp From FragmentB to FragmentA

FragmentB:

isBackpressed = truefindNavController().navigateUp() 

FragmentA:

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

Solution 5:

To pop destinations when navigating from one destination to another, add an app:popUpTo attribute to the associated <action> element. To navigate from fargment2 to Fragment1 with arguments, specify in the navigation graph the action of the caller fragment and the arguments of the destination fragment :

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

In your Fragment2 class, you call your action and pass your argument:

valaction= Fragment2Directions.action2To1("MY_STRING_ARGUMENT")
        findNavController().navigate(action)

Post a Comment for "Pass Data/bundle Using Navigateup In Android Navigation Component"