Understanding Android Activities
In this article, we’ll explore what Android Activities are, how they work, and how to create them.
When you use an app on your Android device, what you’re really interacting with is an Activity.
Activities are the basic building blocks of Android apps, responsible for providing a user interface and handling user interaction.
What is an Activity?
An Activity is a single, focused task that the user can interact with. Each Activity typically corresponds to one screen in your app’s user interface.
For example, if you’re building a messaging app, you might have one Activity for composing messages, another for displaying the user’s inbox, and so on.
When the user launches your app, Android creates an initial Activity to display. From there, the user can navigate to other Activities within your app, either by tapping on buttons or links within the user interface, or by using the device’s back button.
Activities have a lifecycle, which determines how they are created, started, paused, resumed, stopped, and destroyed. This lifecycle is managed by the Android system and is designed to optimize memory usage and improve performance.
Each activity is defined by a Java class that extends the Activity class, and it typically contains a layout file that defines the user interface for the activity. Activities are launched using Intents, which can pass data between activities and trigger different activities based on user input.
Android Activities Lifecycle
Activities have a unique lifecycle that is managed by the Android system.
The lifecycle includes several states, including:
States | Overview |
onCreate() | This method is called when the activity is created. It is typically used to initialize the activity and set up the user interface. |
onStart() | This method is called when the activity becomes visible to the user. It is typically used to prepare the activity to be displayed. |
onResume() | This method is called when the activity is about to start interacting with the user. It is typically used to start animations or other visual effects. |
onPause() | This method is called when the activity is no longer in the foreground, and another activity has taken its place. It is typically used to save any unsaved data or pause any ongoing tasks. |
onStop() | This method is called when the activity is no longer visible to the user. It is typically used to release any resources or stop any ongoing tasks. |
onDestroy() | This method is called when the activity is about to be destroyed. It is typically used to release any resources or perform cleanup tasks. |
onRestart() | The method is invoked when the activity is stopped and restarted. In the case of onRestart(), the activity is in its stopped state, but it has not been destroyed. |
Here is an example of the activity lifecycle in Android:
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "onCreate() called"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart() called"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume() called"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause() called"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop() called"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy() called"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart() called"); } }
Above example extends AppCompatActivity and overrides lifecycle methods. Upon creating an activity, the onCreate() method is called, followed by onStart() and onResume(). OnPause() and onStop() are executed when the activity is paused or stopped.
An activity is destroyed by calling onDestroy(). If the activity is restarted, onRestart(), onStart(), and onResume() are invoked in that order.
Log.d() outputs log messages for each method in the activity lifecycle, allowing you to track the activity lifecycle and see when each method is called.
Starting Activities
Activities can be launched in several ways. One common way is through an intent.
An intent is an object that describes a request to perform an action.
Activities can be launched with an explicit intent, which specifies the target activity, or an implicit intent, which allows the system to choose the appropriate activity.
The syntax for creating an explicit intent in Android is as follows:
Intent intent = new Intent(context, TargetActivity.class); startActivity(intent);
Here, context is the current context of the application, and TargetActivity is the class of the activity that we want to start.
The syntax for creating an implicit intent in Android is as follows:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com")); startActivity(intent);
Here, Intent.ACTION_VIEW is the action that we want to perform, and Uri.parse(“https://www.example.com”) is the data that we want to pass to the activity that will handle the intent.
Managing Activity Transitions
Activity transitions occur when one activity starts another activity.
By default, Android provides system-defined animations for these transitions.
You can also customize these animations using the overridePendingTransition() method.
Here is an example of overriding the default animation for an activity transition:
Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
This code creates a new intent and starts the SecondActivity. The overridePendingTransition() method is then called, passing two animation resources as parameters.
The first animation (slide_in_right) is applied to the new activity as it enters the screen, and the second animation (slide_out_left) is applied to the current activity as it exits the screen.