11

I'm using Peter Doyle's android-support-v4-googlemaps support library for implementing an Activity that uses both Fragments and Google Maps, and can't seem to get FragmentTransaction animations to work. I've tried using the setCustomAnimations(int enter, int exit) method as well as the setTransition(int transit) method but to no avail. Anyone been able to get animations to work, or also had problems getting animations to work?

Some of the animations I tried:

setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)

setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)

setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
animuson
  • 53,861
  • 28
  • 137
  • 147
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • 1
    I believed I had it working on Galaxy S2 but not the others. I'll come back to update you on this when I get to work tomorrow morning. – RobGThai May 29 '12 at 17:00
  • 1
    Check this question out. The accepted answer helped me. http://stackoverflow.com/questions/7718111/android-fragment-standard-transition-not-animating – Sababado Jul 27 '12 at 16:00

3 Answers3

13

You should call FragmentTransaction.setCustomAnimations first and then call FragmentTransaction.replace method like this:

        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.anim.fade_out,R.anim.fade_in);
        ft.replace(R.id.fragmentDetails, detailsFrag);
VSB
  • 9,825
  • 16
  • 72
  • 145
1

Have you tried FragmentTransaction.remove() and then FragmentTransaction.add(), instead of FragmentTransaction.replace()? I've seen in other threads complains about replace() not working as expected.

I haven't used the android-support-v4-googlemaps library, but I can confirm the code below works with android-support-v4.jar:

FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
tx.replace(R.id.fragment_container, new Fragment2());
tx.addToBackStack(null);
tx.commit();
Andrés Pachon
  • 838
  • 1
  • 7
  • 15
  • Thanks for the response Andres. Unfortunately I couldn't get animations to work (except for `setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)` and `setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE)` but encountered other problems with this) so removed the animation/transition method calls. – Adil Hussain May 30 '12 at 11:50
  • Using .add() is better solution than the use of.replace(). A good example is the use of .replace() in support v27.0.0 with setCustomAnimations, app simply crashes when fragment is removed from stack. For me the solution is to use .add(), but the transaction in animation is lost as @AdilHussain said – Pelanes Feb 09 '18 at 09:04
0

Try to make a snapshot of your google map:

private void snapShot() {
    SnapshotReadyCallback callback = new SnapshotReadyCallback() {
        Bitmap bitmap;

        @Override
        public void onSnapshotReady(Bitmap snapshot) {
            // TODO Auto-generated method stub
            bitmap = snapshot;
            try {
                FileOutputStream out = new FileOutputStream(getActivity()
                        .getFilesDir() + "/MapSnapshot.png");
                   bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            } catch (Exception e) {
                   e.printStackTrace();
            }
        }
    };

    map.snapshot(callback);

}

The make a new fragment which have only a picture of the map. Load this new fragment with replace and then make the transition on the fragment you want to replace: final SnapShotFragment snapFrag = new SnapShotFragment(); FragmentTransaction transaction = getFragmentManager() .beginTransaction();

                        transaction.replace(MapFragment.this.getId(),
                                snapFrag);
                        transaction.addToBackStack(null);
                        transaction.commit();
                        getFragmentManager().executePendingTransactions();
                        final boolean roi = isInROI;

                        WayPointDetailActivity waypointFrag = new WayPointDetailActivity();
                        waypointFrag.setWayPointId(wp.getId());
                        waypointFrag.setInRoi(roi);
                        transaction = getFragmentManager()
                                .beginTransaction();

                        transaction.setCustomAnimations(R.anim.enter,
                                R.anim.exit);

                        transaction.replace(snapFrag.getId(), waypointFrag);
                        transaction.addToBackStack(null);
                        transaction.commit();