0

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?

  • This link may be help you https://stackoverflow.com/questions/12529499/problems-with-android-fragment-back-stack – M123 Jan 07 '21 at 05:27
  • Thank you for replying my question, but can't seem to solve my problem with the link provided. My problem lies with the fact that my 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? – Albrito Jan 10 '21 at 20:33

0 Answers0