Android - back button and backstack fragment do not work

I am developing a simple fragment-based application using the FragmentActivity function. Each "screen" of the application is contained in a fragment, and all fragments are added to the layout of the container when the application starts.

// Set up fragments in Main Activity
fragMan = getFragmentManager();
FragmentTransaction ft = fragMan.beginTransaction();
ft.add(R.id.fragment_container, settingsFragment);
ft.add(R.id.fragment_container, mapFragment);
ft.add(R.id.fragment_container, tracksFragment);
ft.add(R.id.fragment_container, waypointsFragment);
ft.commit();

Transitions are performed by hiding the currently visible fragment, and then displaying the corresponding fragment.

ft = fragMan.beginTransaction();
ft.show(mapFragment);
ft.addToBackStack(null);
ft.commit();

Everything works fine, but when the "Back" button is pressed, the application exits, regardless of which screen is visible or what previous transactions have been added to the back stack.

, , , /, , .. , , , / , -, , " " .

, , ( ), -, , , . onBackPressed, , . , , ? , Nexus 7 Jelly Bean.

+5
2

, FragmentActivity ( ) Activity. .

+2

, , , .

( , ), replace , , .

, :

/**
 * Changes the detail fragment of this activity. This is how all content is presented in this app.
 * @param fragment
 * @param animated
 * @param addCurrentFragmentToBackStack
 */
private void changeDetailFragment(Fragment fragment,boolean animated,boolean addCurrentFragmentToBackStack)
{
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (animated)
            transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);

    Fragment currentFrag =  getSupportFragmentManager().findFragmentById(R.id.detailFragment);


    String fragName = "NONE";

    if (currentFrag!=null)
        fragName = currentFrag.getClass().getSimpleName();


    if (currentFrag != null)
    {

        transaction.remove(currentFrag);
    }


    transaction.add(R.id.detailFragment,fragment);


    if (addCurrentFragmentToBackStack)
    {
        Log.i("APP_NAME","Adding: " + fragName + " to the backstack");
        transaction.addToBackStack(null);
    }
    else
    {
        Log.i("APP_NAME","Not adding: " + fragName + " to the backstack");
    }



    transaction.commit();

}

, .

0

All Articles