0

I use Navigation to switch screens.

Move to B fragment on the A fragment screen of the bottom Write menu.

Arguments are also passed while returning back using Safe Args from the moved screen B.

In this state, if i move to another bottom menu and then return to the A screen of the Write Menu, Args is maintained as it is.

I don't know why the args are being persisted, but I don't want this.

When data comes from another screen, null comes and I want the code not to be executed.

I want the A fragment screen to receive data only from the B screen.

For this, I set null as the default value in nav_gaph, but it doesn't make sense because the args are being maintained.

Please tell me the solution and why!

A Fragment

class WriteRoutineFragment : Fragment() {
    private var _binding : FragmentWriteRoutineBinding? = null
    private val binding get() = _binding!!
    private lateinit var adapter : RoutineAdapter
    private val args : WriteRoutineFragmentArgs by navArgs()
    private val vm : WriteRoutineViewModel by activityViewModels { WriteRoutineViewModelFactory() }

    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        _binding = FragmentWriteRoutineBinding.inflate(inflater, container, false)

        adapter = RoutineAdapter(::addDetail, ::deleteDetail)
        binding.rv.adapter = this.adapter
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        args.workout?.let { workout -> // here!! args is maintained..
            vm.addRoutine(workout)
        }

        vm.items.observe(viewLifecycleOwner) { updatedItems ->
            adapter.setItems(updatedItems)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

nav_graph.xml

<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/write_routine_home"
    app:startDestination="@id/writeRoutineHome">

    <fragment
        android:id="@+id/writeRoutine"
        android:name="com.example.lightweight.fragment.WriteRoutineFragment"
        android:label="fragment_write_routine"
        tools:layout="@layout/fragment_write_routine" >
        <action
            android:id="@+id/action_writeRoutineFragment_to_workoutListTabFragment"
            app:destination="@id/workoutListTabFragment" />
        <argument
            android:name="workout"
            app:argType="string"
            app:nullable="true"
            android:defaultValue="@null"/>
    </fragment>
</navigation>
ybybyb
  • 1,385
  • 1
  • 12
  • 33
  • 1
    The question is not very clear. From what I understood, I have made a sample repo for your reference. https://github.com/Abhimanyu14/navigation-demo. Check it out and confirm if that solves your issue. If not, please add more clarity to the question. – Abhimanyu Aug 21 '21 at 18:16
  • thx. thanks. But my code uses activityviewmodels. So, even if you switch to another screen, the received data continues to exist instead of being null. i don't want this... – ybybyb Aug 21 '21 at 18:38
  • 1
    Ok. Got it. Check if my answer helps. – Abhimanyu Aug 22 '21 at 01:49

1 Answers1

1

The issue is not that the args are maintained.
But since you are using activity view models, the data is persistent in the view model.

Use this,

args.workout.let { workout -> // here!! args is maintained..
    vm.addRoutine(workout)
}

The change is that we are not using safe calls(.?) anymore.
Make necessary changes in addRoutine() to accept null values if they don't accept null.

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121