我最近刚刚学习了如何使用片段管理器在两个片段之间发送数据,如下代码所示:
我发送数据的第一个片段:
history_fragment fragment = history_fragment.newInstance(InputValue1, InputValue2, InputValue3, InputValue4);
getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, fragment).commit();
正在接收数据的片段:
public static history_fragment newInstance(String InputValue1, String InputValue2, String InputValue3, String InputValue4) {
history_fragment fragment = new history_fragment();
Bundle args = new Bundle();
args.putInt("Value1", Integer.parseInt(InputValue1));
args.putInt("Value2", Integer.parseInt(InputValue2));
args.putInt("Value3", Integer.parseInt(InputValue3));
args.putInt("Value4", Integer.parseInt(InputValue4));
fragment.setArguments(args);
return fragment;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView value1 = view.findViewById(R.id.Value1);
if ( getArguments() != null) {
Value1 = getArguments().getInt("Value1");
}
value1.setText(String.valueOf(Value1));
}
我的代码还没有完全完成,但我要解决的主要问题是,每当我回到第一个片段时,应用程序就会崩溃。我知道问题是由事务“.replace()”引起的,但是在返回到第一个片段之前,我应该如何停止事务?
暂无答案!
目前还没有任何答案,快来回答吧!