Home » Android » android – Pressing back does not return to previous fragment

android – Pressing back does not return to previous fragment

Posted by: admin June 16, 2020 Leave a comment


I have a problem with adding the fragment transactions to the back stack. I have a Main activity in which I populate my layout with a Menu Fragment:

public class MainActivity extends ActionBarActivity{

protected void onCreate(Bundle savedInstanceState) {
    getFragmentManager().beginTransaction().add(R.id.frag_container, new MainMenuFragment()).commit();
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    return super.onOptionsItemSelected(item);


Then, inside the MainMenuFragment, the user chooses some option which results in replacing the menu fragment with some other fragment:

public class MainMenuFragment extends Fragment implements OnItemClickListener{
    GridView grid;
    FragmentManager manager;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.main_menu_fragment, container, false);

    manager = getActivity().getFragmentManager();
    grid = (GridView) root.findViewById(R.id.gridView1);

    grid.setAdapter(new MenuTileAdapter(getActivity()));

    return root;

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    FragmentTransaction trans = manager.beginTransaction();
    if (position == 0){
        trans.replace(R.id.frag_container, new BasicSettingsFragment());


For what i understand, this should make it so that when the user presses back button on their device, they will be brought back to the menu fragment, but instead this quits the app. What am i doing wrong?

How to&Answers:

Your code is a mixup, you use ActionBarActivity from appcompat and not using getSupportFragmentManager() and the fragments import should be the appcompat one if you decide to use it. If not, use Activity instead of ActionBarActivity and the simple Fragment import with FragmentManager

Add this to your activity android:configChanges="keyboardHidden|orientation|screenSize"
This will stop your activity from restarting when you rotate.
use setRetainInstance(true) on fragments.


In your Activity overwrite:

public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
    } else {

And probably you need to use in every commited fragment transaction:



You are not adding the MainMenuFragment to the back stack. You can try this one on your activity:

R.id.frag_container, new MainMenuFragment()).


When you add or replace a fragment with the FragmentManager, you need to manually add the old fragment to the backstack with addToBackStack() before calling commit().