Home > Mobile >  Android Safe Args Navigation JetPack OnBackPress in Fragment
Android Safe Args Navigation JetPack OnBackPress in Fragment

Time:08-20

I chose the Safe Args method for passing data between fragments because of type-safty.

Sender fragment

        btnNextPage.setOnClickListener{
        val amountTv: EditText = 
view!!.findViewById(R.id.edt_my_argument_view)
        val amount = amountTv.text.toString()
        val action = 
NavFragment01Directions.actionNavFragment01ToNavFragment02(amount)
        Navigation.findNavController(view).navigate(action)
    }

Receiver fragment

private val args: NavFragment02Args by navArgs()
.
.
.
    override fun onViewCreated(view: View, savedInstanceState: 
Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val txtWhole: TextView = view.findViewById(R.id.txt_whole_02)
    val receivedTxt = args.myArg
    txtWhole.text = receivedTxt.toString()
}

My_nav

<?xml version="1.0" encoding="utf-8"?>
  <navigation 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/my_nav"
app:startDestination="@id/navFragment01">
<fragment
    android:id="@ id/navFragment02"
    android:name="alidoran.android.navigation_safe_args.NavFragment02"
    android:label="fragment_nav02"
    tools:layout="@layout/fragment_nav02" >
    <argument
        android:name="my_arg"
        android:defaultValue="Ali"
        app:nullable="true" />
</fragment>
<fragment
    android:id="@ id/navFragment01"
    android:name="alidoran.android.navigation_safe_args.NavFragment01"
    android:label="fragment_nav01"
    tools:layout="@layout/fragment_nav01" >
    <action
        android:id="@ id/action_navFragment01_to_navFragment02"
        app:destination="@id/navFragment02" >
        <argument
            android:name="my_arg"
            app:argType="string"
            android:defaultValue="@null"
            app:nullable="true" />
    </action>

  </fragment>
</navigation>

This is working perfectly.

Now I want to pass a value from the receiver fragment to the sender fragment after pressing the back button.
I know how to do it with Bundle but I want to do it by Safe Args. Also, I know it is possible to create an action for it but I want to handle it in the back press corresponding to the below map. Nav route
As you see we need an action for attaching value(s) but I don't want to create an action from the second one to the first one. I know how to handle it in the back press but I want Safe Args way.

CodePudding user response:

The most common way is to implement FragmentResultListener.
The documentation explain this well, possibly better than I could.

CodePudding user response:

Using navigation component the best way is by savedStateHandle:

findNavController().previousBackStackEntry?.savedStateHandle?.set("key", result)

And observe the response with:

findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<Type>("key")?.observe(viewLifecycleOwner) {result ->
    // the result.
}

As define in the doc:

To pass data back to Destination A from Destination B, first set up Destination A to listen for a result on its SavedStateHandle. To do so, retrieve the NavBackStackEntry by using the getCurrentBackStackEntry() API and then observe the LiveData provided by SavedStateHandle.

https://developer.android.com/guide/navigation/navigation-programmatic#returning_a_result

  • Related