Android Fragment Transitions
Activity and fragment transitions in Lollipop (Android 5.0) are built upon some relatively new Android features called Transitions. Introduced in KitKat, the Transition framework provides a convenient set of APIs for animating between different UI states in an application. This framework is built around two key concepts: scenes and transitions. A scene defines a given state of an application's UI, while a transition defines the animated change between two scenes.
When a scene changes, a transition has two main responsibilities:
* Capture the state of each view in the start and end scenes.
* Create an Animator based on the differences between the views that need to be animated from one scene to the other.
* * *
## Example
This example explains how to use fragment transitions to create custom animations. Let's start by following the steps below:
| Step | Description |
| --- | --- |
| 1 | Use Android Studio to create an Android application, named Fragment Custom Animation, with package name cn.uprogrammer.fragmentcustomanimation. |
| 2 | Modify the res/layout/activity_main.xml file to add a TextView. |
| 3 | Create a layout file named fragment_stack.xml under res/layout/, defining a fragment tag and a button tag. |
| 4 | Create a subdirectory named anim under res/, and add fragment_slide_left.xml, fragment_slide_left_exit.xml, fragment_slide_right_exit.xml, and fragment_slide_left_enter.xml. |
| 5 | In MainActivity.java, you need to add a fragment stack, fragment manager, and onCreateView(). |
| 6 | Launch the Android emulator to run the application and verify the results of the changes made by the application. |
The following is the content of the res/layout/activity_main.xml file, which includes a FrameLayout and a Button.
The following is the content of the res/anim/fragment_stack.xml file:
The following is the content of the res/animator/fragment_slide_left_enter.xml file, which includes set and objectAnimator tags.
The following is the content of the res/animator/fragment_slide_left_exit.xml file, which includes set and objectAnimator tags.
The following is the content of the res/animator/fragment_slide_right_enter.xml file, which includes set and objectAnimator tags.
The following is the content of the res/animator/fragment_slide_right_exit.xml file, which includes set and objectAnimator tags.
The following is the content of the src/cn.uprogrammer.fragmentcustomanimation/MainActivity.java file, which includes a button listener, CountingFragment, and onCreateView():
package cn.uprogrammer.fragmentcustomanimation;import android.app.Activity;import android.app.Fragment;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;/** * Demonstrates using custom animations in a fragment transaction. */public class MainActivity extends Activity { int mStackLevel = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button)findViewById(R.id.new_fragment); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { addFragmentToStack(); } }); if (savedInstanceState == null) { // Add initial fragment Fragment newFragment = CountingFragment.newInstance(mStackLevel); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.add(R.id.fragment1, newFragment).commit(); } else { mStackLevel = savedInstanceState.getInt("level"); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("level", mStackLevel); } void addFragmentToStack() { mStackLevel++; // Instantiate a new fragment Fragment newFragment = CountingFragment.newInstance(mStackLevel); // Add the fragment to the activity, putting it on the back stack FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.setCustomAnimations(R.animator.fragment_slide_left_enter, R.animator.fragment_slide_left_exit, R.animator.fragment_slide_right_enter, R.animator.fragment_slide_right_exit); ft.replace(R.id.fragment1, newFragment); ft.addToBackStack(null); ft.commit(); } public static class CountingFragment extends Fragment { int mNum; /** * Create a new instance of CountingFragment, providing "num" as an argument. */ static CountingFragment newInstance(int num) { CountingFragment f = new CountingFragment(); Bundle args = new Bundle(); args.putInt("num", num); f.setArguments(args); return f; } /** * When creating, retrieve this instance's number from its arguments. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNum = getArguments() != null ? getArguments().getInt("num") : 1; } /** * The Fragment's UI is just a simple text view showing its number. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_stack, container, false); View tv = v.findViewById(R.id.text); ((TextView)tv).setText("Fragment #" + mNum); tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb)); return v; } }}
The following is the content of the AndroidManifest.xml file:
Let's run the modified Fragment Custom Animation application. I assume you have already created an AVD while setting up the environment. Open the activity file in your project and click the  icon in the toolbar to run the application in Android Studio. Android Studio installs the application on the AVD and starts it. If everything goes well, the following will be displayed on the emulator window:

YouTip