10

I'm trying to use the default animations for the Activity with fragments.. here I found something about it:

Android: using Activity's default animation for Fragments

the problem is: ok, I need (for example) "activityOpenEnterAnimation".. how can I use it?

Using the following code won't work:

        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.setCustomAnimations(android.R.anim.activityOpenEnterAnimation, android.R.anim.activityOpenExitAnimation);

        transaction.replace(R.id.container, fragment)
                .addToBackStack(((Object) fragment).getClass().getName())
                .commit();

Hints? Thanks! :)

Community
  • 1
  • 1
Filnik
  • 1,023
  • 3
  • 13
  • 26

3 Answers3

10

Nowadays, Android documentation clearly recommends not to use resources directly from android.R.*, since every release of the platform has changes on them. Even some resources dissapear from one version to another, so you shouldn't rely on them. On the other hand, lots of resources are private and not available from a developer's code.

The safest (and recommended) way is to simply copy & paste the resources you need (in this case, the animations) from the source code of the Android version you want into your own code, and use them through the regular R.*.

You can browse the Android source code in many ways, as explained in [1].

[1] Where can I find Android source code online?

Community
  • 1
  • 1
thelawnmowerman
  • 11,956
  • 2
  • 23
  • 36
  • I've tried with: transaction.setCustomAnimations(R.anim.activity_open_enter, R.anim.activity_open_exit); But it's not the same as in activity... suggestions? – Filnik Mar 27 '14 at 17:16
  • 1
    Search in the source code of the Android platform for `R.anim.activity _open_enter`, and check how it is really used, maybe the only difference is a simple configuration, or using another constructor, etc. – thelawnmowerman Mar 27 '14 at 17:48
  • `Nowadays, Android documentation clearly recommends not to use resources directly` -> You wrote this in 2014. Where is the source for it? I’ve never heard them saying that, in fact, they mostly said: do use the platform values, that’s why we have them! – Martin Marconcini Jul 28 '18 at 00:56
  • It used to say it 4 years ago (not hard to figure that out if you actually saw the dates, BTW...). I don't know if they have recently removed it, but I won't certainly rely on any `android.R.*` resource. I've worked in so many legacy Android apps by now that I don't really need the docs to say it. With "in-home" control over my resources I sleep so much better... The reasons are clearly stated in the answer. Good luck! – thelawnmowerman Jul 28 '18 at 16:00
4

example of using default android animation when back pressed happen

 @Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
}

for using default animations you should use android.R.anim.ANIMATIONNAME

saigopi.me
  • 14,011
  • 2
  • 83
  • 54
2

I managed to get it to work this way:

static public int getResourceIdFromCurrentThemeAttribute(FragmentActivity activity, int attribute){
    TypedValue a = new TypedValue();
    activity.getTheme().resolveAttribute(attribute, a, false);
    return a.resourceId;
}

//This type of fragment will be opened like an activity
static public void openActivityLikeFragment(FragmentActivity activity, BaseFragment fragment, int containerId, String stackRef) {
    FragmentManager fm = activity.getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    //The fragment open/close transition should have the same animations as its activity
    ft.setCustomAnimations(
            getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityOpenEnterAnimation),
            getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityOpenExitAnimation),
            getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityCloseEnterAnimation),
            getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityCloseExitAnimation)
    );
    ft.replace(containerId, fragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(stackRef);
    ft.commit();
}

This solution is safer than directly referencing a resource, since its referencing an attribute which won't change without some deprecation warnings first.

Ricardo Freitas
  • 533
  • 1
  • 6
  • 9
  • 1
    Nice idea, but it doesn't work for me, I always get 0 from resolveAttributes, which Theme are you using? –  Mar 11 '19 at 12:11