0

I have Activity A and B, both inherits from a BaseActivity class. Activity A handles several fragments, and from one of them Activity B is started. In the onCreate method of Activiy B I call the following method :

addFragmentWithAnimation(ExampleFragment.newInstance(), R.anim.custom_slide_in_bottom, 0 , true);

that is received by the Base Activity class:

@Override
public void addFragmentWithAnimation(Fragment fragment, int i, int i1, boolean isInBackStack) {
    FragmentManager fManager = getSupportFragmentManager();
    fManager.executePendingTransactions();
    FragmentTransaction transaction = fManager.beginTransaction();
    transaction.setCustomAnimations(i, i1);
    transaction.add(R.id.container, fragment);
    if (isInBackStack) {
        transaction.addToBackStack(fragment.getClass().getCanonicalName());
    }
    transaction.commit();
}

so, a ExampleFragment is added to the activity and is displayed with a slide in from the bottom animation. Then ExampleFragment call to the same method to a Example2Fragment, but before I remove it as the following

@Override
public void closeFragmentWithAnimation(Fragment fragment, int i, boolean isInBackStack) {
    FragmentManager fManager = getSupportFragmentManager();
    fManager.executePendingTransactions();
    FragmentTransaction transaction = fManager.beginTransaction();
    transaction.setCustomAnimations(0, i);
    transaction.remove(fragment);
    if (isInBackStack) {
        transaction.addToBackStack(fragment.getClass().getCanonicalName());
    }
    transaction.commit();
}

by calling it from Example2Fragment. I'd like to emphasize that both transactions are added to the backstack So far so good, the problem is when I press the back stack button. It returns to a blank activity with no fragment, when the expected behaviour is landing on ExampleFragment

how really works the back stack? What I'm missing here?

thanks

gyosida
  • 450
  • 4
  • 17
  • So why are you calling `addToBackStack` when removing a fragment? Why not [popBackStack()](http://developer.android.com/reference/android/app/FragmentManager.html#popBackStack(java.lang.String,%20int))? – ianhanniballake Jan 08 '15 at 20:18
  • I need to slide the fragment from the top to the bottom when is removed – gyosida Jan 08 '15 at 20:25
  • And what does that have to do with adding an entry into the back stack? – ianhanniballake Jan 08 '15 at 20:26
  • I guess I wanted to record that remove transaction, for when the back key is pressed. However, I've just try with popBackStack, but nothing different happens. At the end a blank activity is shown. – gyosida Jan 08 '15 at 20:30

0 Answers0