How do I return to the previous fragment when I press BackButton between Android Fragments?

Asked 2 years ago, Updated 2 years ago, 43 views

Hello, we are writing code to enable PlaceActivity to switch between InformationFragment and UserInputFragment. By default, PlaceActivity shows the InformationFragment.


        InformationFragment informationFragment = InformationFragment.newInstance(placeId);
        openFragment(informationFragment);

...

    private void openFragment(final Fragment fragment) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_container, fragment);
        ft.commit();
    }

Code that switches to User Input Fragment when you click the button in Information Fragment.

        Button userInput = (Button)v.findViewById(R.id.btn_usr_input);
        userInput.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                InputFragment inputFragment = InputFragment.newInstance(id);
                openFragment(inputFragment);
            }
        });

        return v;
    }

    private void openFragment(final Fragment fragment) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_container, fragment);
        ft.commit();
    }

Click the button after user input to return to the Information Fragment. The following codes are implemented in User Input Fragment:

       submitBT.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                String date = dateET.getText().toString();
                String score = String.valueOf(ratingRB.getRating());
                String userInput = opinionET.getText().toString();

                if (selectedImage == null) {
                    placeDAO.updatePlace(id, 1, date, score, userInput, null);
                } } else {
                    placeDAO.updatePlace(id, 1, date, score, userInput, selectedImage.toString());
                }
                // Update DB

                InformationFragment informationFragment = InformationFragment.newInstance(id);
                openFragment(informationFragment);
            }
        });

        return v;
    }

    private void openFragment(final Fragment fragment) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_container, fragment);
        ft.commit();
    }

This will switch back to Information Fragment when the input button is pressed. The problem is when BackButton is pressed, not back to the previous Fragment, but to exit Activity and return to the previous Activity. Is there any way to return to the previous fragment when BackButton is pressed?

android fragment

2022-09-21 20:28

1 Answers

Call the addToBackStack() function before committing the transaction as shown in the sample code below.

Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();

This function stores the previous state in the back stack. This backstack is managed by the activity and returns to the previous fragment when the back button is clicked.

For more information, read the Fragment Transaction Execution section of the following link.


2022-09-21 20:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.