Android Development
Activity Lifecycle
Mobile App Programming
Activity Transition
Android Studio

Activity transition in Android

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Activity transitions in Android control how one screen enters while another exits. Good transitions make navigation feel intentional, while bad ones feel jarring or slow. The core choices are simple custom animations, shared element transitions, and using the right API so the effect works consistently across the app.

Basic Activity Launch

At the simplest level, one activity starts another with an Intent.

kotlin
1import android.content.Intent
2import androidx.appcompat.app.AppCompatActivity
3
4class MainActivity : AppCompatActivity() {
5    fun openDetails() {
6        val intent = Intent(this, DetailActivity::class.java)
7        startActivity(intent)
8    }
9}

That alone works, but it uses the default transition chosen by the system theme.

Apply Custom Enter and Exit Animations

If you want a specific slide or fade, provide animation resources and apply them when launching the new activity.

kotlin
1import android.content.Intent
2
3val intent = Intent(this, DetailActivity::class.java)
4startActivity(intent)
5overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)

Example animation file:

xml
1<?xml version="1.0" encoding="utf-8"?>
2<translate xmlns:android="http://schemas.android.com/apk/res/android"
3    android:duration="250"
4    android:fromXDelta="100%"
5    android:toXDelta="0%" />

You normally define two animations:

  • how the new activity enters
  • how the current activity exits

The same applies when finishing:

kotlin
finish()
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right)

That keeps forward and backward navigation visually consistent.

Use Shared Element Transitions for Visual Continuity

When one view on the first screen should appear to continue into the second screen, shared element transitions work better than generic slides.

kotlin
1import android.app.ActivityOptions
2import android.content.Intent
3import androidx.core.util.Pair
4
5val intent = Intent(this, DetailActivity::class.java)
6val options = ActivityOptions.makeSceneTransitionAnimation(
7    this,
8    Pair(imageView, "hero_image")
9)
10
11startActivity(intent, options.toBundle())

The destination activity needs a matching transition name:

kotlin
imageView.transitionName = "hero_image"

This is especially effective for gallery, profile, and card-to-detail flows.

Theme-Based Window Transitions

Android also supports transitions through window theme attributes. That is useful when you want consistent activity behavior without repeating code in every launch call.

xml
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:windowActivityTransitions">true</item>
</style>

With theme-based transitions, you can define enter and exit behavior at the window level and keep launch code cleaner.

Know When to Use Fragment Transitions Instead

Not every screen change should be an activity transition. If the navigation happens inside one activity, fragment transitions are often a better fit. They are usually easier to coordinate with shared view models, bottom navigation, and modern single-activity app structures.

Use activity transitions when:

  • the app truly moves between activities
  • the screens have distinct lifecycle boundaries
  • the architecture already uses multiple activities

Use fragment or Compose navigation transitions when the flow stays inside one host activity.

Keep Transitions Fast and Consistent

Good transition design is more about restraint than novelty. Fast, consistent animations feel better than long dramatic ones.

Practical guidelines:

  • keep durations short
  • animate a small number of elements
  • use the same direction logic across similar screens
  • test on low-end devices

A slow transition that blocks interaction is worse than no transition at all.

Common Pitfalls

One common problem is defining nice animation XML but forgetting to call overridePendingTransition, so the custom resources are never used.

Another issue is using heavy animations on complex layouts, which can cause dropped frames and make navigation feel slower.

Shared element transitions also fail easily if transition names do not match exactly between source and destination views.

Summary

  • Activity transitions define how one Android screen enters and another exits.
  • Use overridePendingTransition for straightforward custom enter and exit animations.
  • Use shared element transitions when a specific view should visually continue across screens.
  • Prefer fragment or Compose transitions if the app flow stays inside one activity.
  • Keep animations short, consistent, and light enough to stay smooth.

Course illustration
Course illustration

All Rights Reserved.