0

I have an app with 3 tabs. For two of theese tabs I have buttons where I change the current fragment with a new one with this code:

MapFragment newFragment = new JourneyMapFragment(mContext, getFromDestinationCoordinate(), getToDestinationCoordinate());
                android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.setCustomAnimations(android.R.animator.fade_in,
                        android.R.animator.fade_out);

                // Replace whatever is in the fragment_container view with this fragment,
                // and add the transaction to the back stack
                transaction.replace(R.id.fragment_container, newFragment);
                transaction.addToBackStack(null);

                if(newFragment.isHidden()){
                    transaction.show(newFragment);
                }

                transaction.commit();

For on of the tabs which is a normal fragment, changing into a map fragment the back button takes me back to the original fragment with no issuse.

However, another tab which is a mapfragment, changing into a normal fragment does not give me the same action when pressing the back button. When it's pressed it changes the view to a white/black view.

This is the transaction code within the tab where the back button won't work:

                        Fragment newFragment = new CloseBusStopFragment(mContext, busStopList, getMyPosition());
                        android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
                        transaction.setCustomAnimations(android.R.animator.fade_in,
                                android.R.animator.fade_out);

                        // Replace whatever is in the fragment_container view with this fragment,
                        // and add the transaction to the back stack
                        transaction.replace(R.id.fragment_container, newFragment);


transaction.addToBackStack(null);

                    if(newFragment.isHidden()){
                        transaction.show(newFragment);
                    }
                        // Commit the transaction
                        transaction.commit();

Anyone know why this happens? Any help?

Maggie
  • 33
  • 2
  • 6
  • I'm not sure if I understand. But I think you are adding your first fragment transaction to the backstack. – nicous Apr 25 '13 at 12:31
  • posted the transaction code for the fragment where the back button don't work too. I don't know. Look exactly the same to me... – Maggie Apr 25 '13 at 13:01

1 Answers1

0

If you can do that manually, you can try overriding the onBackPressed() and do whatever you want manually.

   @Override
   public void onBackPressed() {
                   // Do whatever your transaction manually
        super.onBackPressed();

    }
Ajith M A
  • 3,838
  • 3
  • 32
  • 55
  • I've added that one with no luck. I just tried to remove the map in the fragment I want to "go back to" and then the back button works perfectly. So there must be an issue there – Maggie Apr 25 '13 at 13:06