[英]Can't pass data from one fragment to another through bundle in setGraph of navController
我将在片段之间传递数据; 我通过使用 bundle 并将其放入 setGraph 参数来执行此操作,但我无法在另一个片段中获取它,并且它总是返回 null。 问题是什么?
片段导航
Bundle bundle = new Bundle();
bundle.putString("deviceSerialNo", "abc");
Log.d("TAGTAG", "onClick: " + bundle);
navController.setGraph(R.navigation.nav_graph, bundle);
navController.navigate(R.id.action_FirstFragment_to_SecondFragment);
第二个片段
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
Log.d("TAGTAG", "onCreateView: " + getArguments());
if (getArguments() != null) {
String string = getArguments().getString("deviceSerialNo");
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false);
}
根据setGraph()
文档,您传入的Bundle
称为startDestinationArgs
:
startDestinationArgs
: arguments 发送到图的起始目的地
因此,使用带有Bundle
的setGraph
的唯一原因是,如果您的FirstFragment
需要接收 arguments 的bundle
。 作为navigate()
调用的一部分,您需要手动将这些 arguments 传递给任何其他片段,例如SecondFragment
。
// Pass the arguments to the SecondFragment
navController.navigate(R.id.action_FirstFragment_to_SecondFragment, bundle);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.