Today, I came across the term “Trampoline Activities”. Although it’s not an official name (or is it?), I’ve noticed it is a typical pattern and have decided to make a “How To” guide.
What are Trampoline Activities?
A Trampoline Activity is an Activity
that launches another activity and finishes itself. It may include conditional logic to determine which activity to launch or transforming the parameters before sending it to the next Activity
.
Here is an example: Trampoline Activity.
How to create a Trampoline Activity?
To create a Trampoline Activity, you need an activity without a UI that closes itself and moves on to the next activity. The key is to use the NoDisplay
theme to remove the UI:
<activity
android:name=".MyTrampolineActivity"
android:theme="@android:style/Theme.NoDisplay" />
You can write your logic in the onCreate
method of MyTrampolineActivity
and launch the next activity as soon as possible. Remember to finish the MyTrampolineActivity
at the end of the onCreate
method:
class MyTrampolineActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Start your next activity.
finish()
}
}
Note that the NoDisplay
theme means no UI, including toasts or dialogs. If you don’t call finish()
before onResume()
completes, the activity will crash on any Android 23+ device. The behaviour is intentional, as you can see here and here:
PSA: The new requirement to immediately finish an activity if using Theme.NoDisplay is not a regression, this has always been a requirement of it (see https://developer.android.com/reference/android/R.style.html#Theme_NoDisplay for example).
The reason the platform in M is now crashing the app if it doesn’t use this is because not using it would previously break in very subtle and mysterious ways. For example, you would sometimes end up with your app ANRing for no reason.
If you want a transparent Activity
, consider using the Translucent
theme instead.
When should you use a Trampoline Activity?
Avoid using Trampoline Activities in every situation (or even better, use a Single Activity) and always consider alternative solutions before relying on them. However, there are some scenarios in which it can be helpful, including:
- When you need to access Activity context APIs, from a different context.
- When you want to transform or sanitize parameters, before passing them forward to the next Activity.
- When you need to perform conditional routing logic between multiple activities.
ℹ️ To stay up to date with my writing, follow me on Twitter or Mastodon. If you have any questions or I missed something, feel free to reach out to me! ℹ️