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();
    }
}
6
  • Could someone help me please? Commented Jan 25, 2015 at 5:13
  • From above code DetailFragment object never null, because before if condition you are creating object for DetailFragment. Sol always if() will execute. Commented Jan 25, 2015 at 5:35
  • Please check this answer stackoverflow.com/questions/27717127/… Commented Jan 25, 2015 at 5: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 Commented Jan 25, 2015 at 6:40
  • You have to do onBackPressed() in an activity stackoverflow.com/questions/27717127/… Commented Jan 25, 2015 at 6:50

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.