Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 fragment? navigateUp() does not have any facility to pass data to previous fragment. Even I did not find solution using Safe Args. It's passing data forward. I want to have in backward Frad B -> Frag A.

My code to go back to previous fragment

Navigation.findNavController(view).navigateUp()

enter image description here

My question is, How can i get data in previous fragment. I can navigate to Frag A from Frag B using

like image 749
Bhavesh Hirpara Avatar asked Mar 08 '19 07:03

Bhavesh Hirpara


People also ask

How do you pass data between bundles using fragments?

Therefore, in order to pass your data to the Fragment being created, you should use the setArguments() method. This methods gets a bundle, which you store your data in, and stores the Bundle in the arguments. Subsequently, this Bundle can then be retrieved in onCreate() and onCreateView() call backs of the Fragment.

Which method used to pass data between activities?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How can we send data from one fragment to another?

Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Create two FragmentActivity and add the codes which are given below.


2 Answers

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:
class SharedViewModel : ViewModel() {
    val dataToShare = MutableLiveData<String>()

    fun updateData(data: String) {
        dataToShare.value = data
    }
}
  1. Observe data changes in Fragment1:
class Fragment1 : Fragment() {

    private lateinit var viewModel: SharedViewModel

    override fun onViewCreated(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:
class Fragment2 : Fragment() {

    private lateinit var viewModel: SharedViewModel

    override fun onViewCreated(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.

like image 63
Natig Babayev Avatar answered Oct 17 '22 23:10

Natig Babayev


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

like image 7
Mahdi Nouri Avatar answered Oct 17 '22 23:10

Mahdi Nouri