How to use default animation in Android?

I am trying to use the default animation for Activity with fragments. Here I found something about this:

Android: using default animation for fragments

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

Using the following code will not 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(); 

Tips? Thank you :)

+6
source share
3 answers

Currently, the documentation for Android clearly does not recommend using resources directly from android.R.* , Since there are changes to them on each version of the platform. Even some resources disappear from one version to another, so you should not rely on them. On the other hand, many resources are private and inaccessible from developer code.

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

You can view the Android source code in different ways, as described in [1].

[1] Where can I find Android source code on the Internet?

+8
source

I managed to get it to work as follows:

 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 accessing the resource, because it refers to an attribute that will not change without any warning about obsolescence.

0
source

example of using default android animation when clicking back

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

to use default animations you have to use android.R.anim.ANIMATIONNAME

0
source

All Articles