5

I have my MainActivity that manages two fragments working together. One of my methods is a listener interface on my ListFragment and the MainActivity is in charge of switching the fragments.

But for some reason it seems like the addToBackStack doesnt work. When I tap on the list, go to the next fragment and tap the back button of the device...it just goes outside of the App, on the device home screen.

Anyone knows what the issue is?

@Override
public void OnSelectionChanged(Object object) {
    DetailFragment DetailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detail_fragment);

    if (DetailFragment != null) {
        DetailFragment.setTitle(object);
    } else {
        DetailFragment newDetailFragment = new DetailFragment();
        Bundle args = new Bundle();

        args.putSerializable(DetailFragment.KEY_POSITION,object);
        newDetailFragment.setArguments(args);
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

        fragmentTransaction.replace(R.id.fragment_container, newDetailFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}
Makoto
  • 1,455
  • 4
  • 13
  • 17
  • Could someone help me please? – Makoto Jan 25 '15 at 05:13
  • From above code DetailFragment object never null, because before if condition you are creating object for DetailFragment. Sol always if() will execute. – sandeepmaaram Jan 25 '15 at 05:35
  • Please check this answer http://stackoverflow.com/questions/27717127/how-to-handle-backpress-with-fragment/27717201#27717201 – sandeepmaaram Jan 25 '15 at 05:36
  • I have replaced the null by a String (but I never had to do that before to get it work) but still doesnt work. And I'm calling addToBackStack() before the commit – Makoto Jan 25 '15 at 06:40
  • You have to do onBackPressed() in an activity http://stackoverflow.com/questions/27717127/how-to-handle-backpress-with-fragment/27717201#27717201 – sandeepmaaram Jan 25 '15 at 06:50
  • No, didnt work unfortunately – Makoto Jan 25 '15 at 07:50

2 Answers2

4

You should add this method in your MainActivity.

@Override
public void onBackPressed() {
    FragmentManager fm = getFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        fm.popBackStack();
    } else {
        super.onBackPressed();
    }
}

And check your imports to use android.app. instead of android.support.v4.app.

For example:

import android.app.FragmentManager;

instead of :

import android.support.v4.app.FragmentManager;
Floern
  • 33,559
  • 24
  • 104
  • 119
sevshum
  • 41
  • 4
1

If you are using extends AppCompatActivity in your activity go ahead and use the imports from the support library.

import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction;

then change your calls from

getFragmentManager();

to

getSupportFragmentManager();

This should work fine. As I just had to fix this in one of my projects as well. I learned about this here https://teamtreehouse.com/library/android-fragments/managing-fragments/managing-the-back-stack

However it's a paid online learning solution so I am unsure if you'll be able to see it. They occasionally have some videos open to the public.

If you are using extends Activity and want to continue using getFragmentManager();

then you'll want to use

import android.app.FragmentManager;

You should need to have to override the onBackPressed() method. While it does work in these cases it's more of a hack than a solution.

john.weland
  • 266
  • 2
  • 12