I have recently just learnt how to use a fragment manager to send data between two fragments as shown in the codes below:
My First fragment that I am sending the data from:
history_fragment fragment = history_fragment.newInstance(InputValue1, InputValue2, InputValue3, InputValue4);
getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, fragment).commit();
The Fragment that is receiving the data:
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));
}
My code is not fully complete but the main problem am I trying to solve is that whenever I go back to my first fragment, the application crashes. I know that the problem is due to the transaction ".replace()", but how should I stop the transaction before returning back to the first fragment?