Reputation: 1739
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>
Upvotes: 0
Views: 243
Reputation: 14787
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
.
Upvotes: 1