Support library: FragmentTransaction animations not working
Asked Answered
T

3

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)
Tito answered 26/1, 2012 at 10:33 Comment(2)
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.Parkman
Check this question out. The accepted answer helped me. #7718611Smasher
H
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);
Hazardous answered 22/9, 2013 at 16:53 Comment(0)
S
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();
Sinistrous answered 29/5, 2012 at 16:42 Comment(2)
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.Tito
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 saidAristippus
M
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();
Mistrot answered 12/6, 2014 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.