简体   繁体   中英

Adding Fragment to the addToBackstack when you have a single activity with 2 fragments

Android Studio 1.1.2

Hello,

I have MainActivity, TitlesFragment, and DetailsFragment.

My MainActivity that will display TitlesFragment and when the user clicks a title the TitlesFragment will be replaced with the DetailsFragment. That works fine. However, When I add the DetailsFragment to the addToBackStack. When I click the back button, instead of displaying the TitlesFragment it will close the app.

I am thinking for I need 2 Activity for this. One for the TilesFragment and another for the DetailsFragment? Currently I am trying to do this with a single activity?

Current this is my code snippet for MainActivity (only showing the fragment transactions)

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");

        setContentView(R.layout.activity_main);

        /* Load ListFragment */
        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_container);

        /* If the fragment is null then we need to load this fragment */
        if(fragment == null) {
            fragment = TitlesFragment.newInstance(1234);
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            fragmentTransaction.add(R.id.fragment_container, fragment, "TitlesFragment");
            fragmentTransaction.commit();
        }
    }

    @Override
    public void onColorPlateSelected(long plateID) {
        Log.d(TAG, "onColorPlateSelected: " + plateID);
        /* Get the plate ID and pass it to the details fragment */

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        /* Replace the fragment with the new color plate */
        fragmentTransaction.replace(R.id.fragment_container, DetailsFragment.newInstance(plateID), "DetailsFragment");
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        fragmentTransaction.commit();
    }

My layout for activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

Here the ActivityMain will replace the fragment in the framelayout.

I have seen this do by handling the OnBackPressed event handler. But I am wondering if I can do it without that?

Many thanks for any suggestions,

Try just adding the same addToBackStack() method to your first list fragment that you add to your activity. ie

fragment = TitlesFragment.newInstance(1234);
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            fragmentTransaction.add(R.id.fragment_container, fragment, "TitlesFragment");

            fragmentTransaction.addToBackStack(null);

            fragmentTransaction.commit();

This should then prevent the second fragment replacing it in the back stack.

I have managed to solve the issue. My main activity's signature was this:

public class MainActivity extends ActionBarActivity  {
   ..
}

I was extending the MainActivity to inherit from the ActionBarActivity. However, when I changed the class signature to this:

public class MainActivity extends Activity  {
    ..
 }

Everything worked as expected and I was able to navigate to the titlesFragment from the detailsFragment using the backButton.

Not sure why this is, maybe when using fragments the actionbar will be better off implemented in the fragments themselves and rather than in the main activity.

Hope this can help someone else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM