Back doesn't work when I want to go back to the previous snippet

I am managing 2 Fragments in my MainActivity.

One of them is a ListFragment subclass for displaying a list of items.

The basic idea is to navigate to another list when the user clicks on one of the items, and the user can return to the previous list by pressing the back button.

The code to pass to the new list is shown as follows:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
NewFragment newFragment = new NewFragment();
newFragment.setArguments(getIntent().getExtras());
transaction.replace(R.id.fragment_layout, newFragment);
transaction.addToBackStack(null);
transaction.commit();

      

However, I just just quit the application other than going back to the previous view. What am I doing wrong?

+3


source to share


2 answers


unlike actions, with fragments you must explicitly add things to the "back stack". basically when your application displays a new snippet that you need to return to the preview snippet you call FragmentTrasaction.addToBackStack()

.

frameworks frame a snippet from the back stack when the user pushes back. if you need something a little more complex, you can undo the Back button for your snippet. this question covers that



Android Fragment return button click

0


source


The Back button closes the top activity and does not return to the fragment history. You have to do it yourself with popBackStack ()



-2


source







All Articles