Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Using Transitions Between Activitities

Author: Ian Darwin
Published? false
FormatLanguage: WikiFormat

Problem:

You want to assume control for the transitions between Activities, instead of allowing the default "instant" transition.

Solution:

Use the Activity class; setDeferredTransition() method.

Discussion:

The normal flow between screens of an application is by starting and ending instances of classes that extend Activity. When you move between screens this way, there is no control over the visual effect of the transition. To provide control over the appearance of the transition, the Activity class as of Android 3 (API Level 11) offers the setDeferredTransition() method. This takes two arguments, the transition when moving into this activity and the transition when moving out from it. Transitions are normally set up in XML as animations and/or fade in/fade outs. A good starter set of transitions and animations is included in the SDK, in the UIDemo sample project. You can simply copy all the files in ${SDK_HOME}/android/${API_LEVEL}/src/UIDemo/res/anim (where SDK_HOME is of course where you installed the Android SDK, and API_LEVEL is an existing folder there such as android-11) into your project's res/anim folder. Refresh the project. If you get a bunch of syntax errors, you probably copied from a sample project that is later than your current project's target SDK level. Delete the ones that are invalid.

Then in your activity's onCreate() method, set the transitions as follows:

XXX

Note that if you have a variety of Activity classes (e.g., a real application) you probably want to ensure that the animations are the same for all, to give your app a consistent look. In this case, a technique I've used is to make a new base class which extends Activity and provides the transitions, then make all the normal activity classes extend this.



public class MyStuffActivity extends TransitionActivity {
...
}

If you have a lot of ListActivity subclasses you'd probably also want to provide the obvious TransitionListActivity base class as well.

Prior to Android 3, it was common to use the ViewFlipper class for this purpose. The drawback of this is that all the views take place in the same Activity, which unduly convolutes the code (basically making you put all your Activities into one class, or at least, create them and configure them. Not so nice.

See Also:

ViewFlipper in 2871 where it is used to display different views based on Gesture Recognition.

No records found.