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:
livefeed_fragment fragment = livefeed.newInstance(InputValue1, InputValue2, InputValue3, InputValue4);
getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, fragment).commit();
Second Fragment that is receiving the data:
public static livefeed_fragment newInstance(String InputValue1, String InputValue2, String InputValue3, String InputValue4) {
livefeed_fragment fragment = new livefeed_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 problem lies with the fact that the first fragment is constantly replacing itself in order to update the new sets of data that come in. Hence, due to this constant replacement of the fragment, if I were choose to navigate to a new fragment, say "Fragment 3", the app crashes. I want to physically stop this replace() command, is there a way to do that?