假设我有三个片段,A、B、C;
A -> B <-> C
B和C之间是循环关系。B 或 C 片段都需要arguments,例如
val args = Bundle()
args.putString("StringKeyBC", argValueBtoC)
findNavController().navigate(R.id.action_fragmentB_to_fragmentC, args, null)
args.putString("StringKeyCB", argValueCtoB)
findNavController().navigate(R.id.action_fragmentC_to_fragmentB, args, null)
Run Code Online (Sandbox Code Playgroud)
问题是每次我在 B 和 C 之间移动时,片段都会添加到后堆栈中,我不希望那样。如果片段已经回到堆栈,我只想弹出它,但如果我使用,popBackStack我不能再添加参数:
public boolean popBackStack(@IdRes int destinationId, boolean inclusive)
Run Code Online (Sandbox Code Playgroud)
那么,如何在不每次都将它们添加到后堆栈的情况下在两个片段之间不断切换?
navigation android android-fragments kotlin android-architecture-navigation
我想在有backpress / manual back发生时获取数据。我navigateUp()过去经常回去。如何将数据传递到上一个片段?navigateUp()没有任何工具可以将数据传递到上一个片段。甚至我也没有找到使用Safe Args的解决方案。它正在转发数据。我想在向后的Frad B-> Frag A中使用。
我的代码回到上一个片段
Navigation.findNavController(view).navigateUp()
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何获取上一个片段中的数据。我可以使用以下方法从Frag B导航至Frag A
android android-fragments android-architecture-navigation android-safe-args
我正在使用导航组件,我有Fragment A和Fragment B,从Fragment A我使用安全参数将一个对象发送到Fragment B并导航到它。
override fun onSelectableItemClick(position:Int,product:Product) {
val action = StoreFragmentDirections.actionNavigationStoreToOptionsSelectFragment(product,position)
findNavController().navigate(action)
}
Run Code Online (Sandbox Code Playgroud)
现在,在我的Fragment B中进行一些逻辑之后,我想再次将该数据传递给我使用的Fragment A
btn_add_to_cart.setOnClickListener {button ->
findNavController().previousBackStackEntry?.savedStateHandle?.set("optionList",Pair(result,product_position))
findNavController().popBackStack()
}
Run Code Online (Sandbox Code Playgroud)
然后在片段 A中,我用
findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<Pair<MutableList<ProductOptions>,Int>>("optionList")
?.observe(viewLifecycleOwner, Observer {
storeAdapter.updateProductOptions(it.second,it.first)
})
Run Code Online (Sandbox Code Playgroud)
现在,这工作正常,但如果我从片段 A转到片段 B并按后退按钮,上面的观察者会再次触发,复制我当前的数据,有没有办法在我只按下片段btn_add_to_cart中的按钮时触发该观察者乙?
android android-fragments kotlin android-viewmodel android-architecture-navigation
我的应用程序从NewAdFragment这个片段开始,里面有一个按钮,允许用户选择类别,当用户单击该按钮时,我将通过ChooserFragment传递类别列表作为参数将他导航到,这个片段中有 RecyclerView ,它显示类别。
我想要什么?我希望当用户从 RecyclerView 中选择一个项目时,它必须返回NewAdFragment特定的数据。
我可以用很多方法解决这个问题,但也许导航组件库中已经存在一种方法。
最好的方法是什么?