Fragment Back Button Error

Asked 2 years ago, Updated 2 years ago, 132 views

There are two Fragments in total. Code that moves from fragment2 to fragment1.

faragment2.java

                Fragment1 fragment1 = new Fragment1();
                Bundle bundle = new Bundle();
                bundle.putString("mkey",key);
                fragment1.setArguments(bundle);

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.replace(R.id.container,fragment1);
                fragmentTransaction.commit();

If you press the back button on fragment 1, it will move back to fragment 2 The result is fragment2 and the app will exit as soon as it appears on the screen. I can't find a case like this, so I'm asking a question...

fragmenttransaction

2022-09-21 21:28

1 Answers

There is no problem with the code you uploaded. On the code, addToBackStack() is used to return to the previous fragment when the back button is clicked.

Does the app you mentioned mean that it will be forcibly terminated by Exception? If so, it would be better to check the log that is printed as a logcat. The app seems to be shutting down for another reason.


2022-09-21 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.