A fundamental part of Android development is understanding how Activities operate. Each screen in your Android app is associated with an Activity, and every Activity has a lifecycle that is key to maintaining a seamless and bug-free user experience. This article will help you understand the lifecycle of an Activity, the Activity Stack, and the importance of handling these correctly in your Android apps.
Getting Acquainted with Activity Lifecycle
An Activity’s lifecycle is defined by a series of callbacks allowing the developer to know when the Activity is created, stopped, resumed, or destroyed. Here are the core stages in an Activity lifecycle:
- onCreate(): This is the first call in the lifecycle of an Activity. Here you typically do your static setup — create views, bind data to lists, etc. Always followed by onStart().
- onStart(): Called when the Activity becomes visible, but it might be covered by another Activity (an overlay activity). Always followed by onResume() if the Activity comes to the foreground, or onStop() if it becomes hidden.
- onResume(): Called when the Activity is in the foreground, or the user can interact with the Activity. Always followed by onPause().
- onPause(): Called when the system is about to resume a previous Activity. This method is typically used to commit unsaved changes to persistent data, stop animations, and other things that might be consuming CPU. Always followed either by onResume() if the Activity returns back to the front, or by onStop() if it becomes invisible to the user.
- onStop(): Called when the Activity is no longer visible. If the Activity is being destroyed, then onDestroy() is called immediately after.
- onDestroy(): Called just before the Activity is destroyed. This can occur because the Activity is finishing (due to the user completely dismissing the Activity or due to
finish()
being called on the Activity), or because the system is temporarily destroying the Activity due to a configuration change (such as rotating the screen).
By understanding and properly handling these callbacks, you can ensure your app behaves as expected when the user navigates through it, or when the system interrupts it (like for an incoming phone call).
Getting to Know the Activity Stack
The Activity Stack, also known as the “back stack,” is the sequence of Activities a user has visited. Android keeps track of this navigation history for you within a Task. As a developer, it’s important to know that Android manages the Activity stack for you, meaning when the user navigates through your app, Android takes care of pushing and popping Activities on and off the stack.
However, you do have control over how Android associates your Activities with Tasks and how it arranges them in the Activity Stack. You can specify this behavior in your manifest file using android:taskAffinity
, android:launchMode
, android:allowTaskReparenting
and other attributes.
The Significance of Save and Restore States
When Android shuts down an Activity due to a configuration change (like rotating the screen), or to reclaim resources, it’s vital to save the Activity’s state so that you can restore it when the Activity is recreated. This is usually done by saving the state in the onSaveInstanceState()
method.
This bundle will then be passed to both the onCreate()
method and the onRestoreInstanceState()
method so you can restore your state.
Example:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back!");
}
Then in onCreate()
or in onRestoreInstanceState()
you can read these values:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}
Conclusion
Understanding the Activity Lifecycle and Activity Stack is crucial in Android development. By correctly managing Activities and states, you can provide a fluid, efficient, and bug-free experience for your users. Take your time to understand these concepts fully and apply them effectively in your Android development journey.