Question: [I found the question but does not have solution in code](https://stackoverflow.com/q/53298615/1136023)

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](https://developer.android.com/topic/libraries/architecture/navigation/navigation-pass-data#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](https://i.stack.imgur.com/b27YH.png)](https://i.stack.imgur.com/b27YH.png)

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

Answer: 
According to [developer.android.com](https://developer.android.com/topic/libraries/architecture/viewmodel#sharing), you can use common for fragments where you want to share data [ViewModel](https://developer.android.com/reference/android/arch/lifecycle/ViewModel.html) 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
    }
}

```

2. 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
        })
    }
}

```

3. 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.
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 :

```
<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>

```

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

```
   val action = Fragment2Directions.action2To1("MY_STRING_ARGUMENT")
        findNavController().navigate(action)

```