问题描述
我使用Navigation
来切换屏幕.
移动到底部写入菜单
的A fragment
屏幕上的B fragment
.
在使用 Safe Args
从移动的屏幕 B
返回时也传递参数.
Arguments are also passed while returning back using Safe Args
from the moved screen B
.
在这种状态下,如果我移动到另一个底部菜单
,然后返回到Write Menu
的A
屏幕,Args
保持原样.
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.
我不知道为什么要保留 args,但我不想要这个.
I don't know why the args are being persisted, but I don't want this.
当数据来自另一个屏幕时,null
来了,我希望代码不被执行.
When data comes from another screen, null
comes and I want the code not to be executed.
我希望 A
片段屏幕仅从 B
屏幕接收数据.
I want the A
fragment screen to receive data only from the B
screen.
为此,我在 nav_gaph
中将 null
设置为 default value
,但这没有意义,因为正在维护 args.
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!
片段
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>
推荐答案
问题不在于 args 的维护.
但是由于您使用的是活动视图模型,因此数据在视图模型中是持久的.
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.
使用这个,
args.workout.let { workout -> // here!! args is maintained..
vm.addRoutine(workout)
}
变化是我们不再使用安全调用(.?
).
在 addRoutine()
中进行必要的更改以接受 null
值,如果它们不接受 null
.
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
.
这篇关于切换屏幕时Safe Args的参数继续存在的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!