In the Android platform, Activity is a key component that constitutes the user interface.
An activity can be viewed as a screen where the user can perform specific tasks and represents the basic building block of an Android application.
What is an Activity?
An activity is a screen that displays a specific task to the user. For example, screens where users can click buttons or
input information are all considered activities. Each activity exists independently and allows users
to perform various tasks by switching between multiple activities.
Activity Lifecycle
An activity has states such as creation, start, pause, resume, stop, and destruction, and these states are managed through
the activity’s lifecycle. Understanding the activity lifecycle is crucial for app development.
Lifecycle Methods
onCreate()
: Called when the activity is first created. Initializes UI and performs setup tasks.onStart()
: Called when the activity is about to become visible to the user.onResume()
: Called when the activity starts interacting with the user.onPause()
: Called when another activity is about to start interacting with the user. Used to save important tasks.onStop()
: Called when the activity is no longer visible to the user.onDestroy()
: Called when the activity is finishing. Used to clean up remaining resources to prevent memory leaks.
Activity Example
Below is a sample code that creates a basic activity and implements lifecycle methods.
In this example, the current state will be displayed in a TextView.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
textView.setText("onCreate called");
}
@Override
protected void onStart() {
super.onStart();
TextView textView = findViewById(R.id.textView);
textView.setText("onStart called");
}
@Override
protected void onResume() {
super.onResume();
TextView textView = findViewById(R.id.textView);
textView.setText("onResume called");
}
@Override
protected void onPause() {
super.onPause();
TextView textView = findViewById(R.id.textView);
textView.setText("onPause called");
}
@Override
protected void onStop() {
super.onStop();
TextView textView = findViewById(R.id.textView);
textView.setText("onStop called");
}
@Override
protected void onDestroy() {
super.onDestroy();
TextView textView = findViewById(R.id.textView);
textView.setText("onDestroy called");
}
}
Communication Between Intents and Activities
In Android, transitions between activities occur through Intents.
An intent is an object used to pass information from one activity to another or to call system components.
Intent Usage Example
Let’s look at sample code that creates two activities, where clicking a button in the first activity
transitions to the second activity.
// MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
});
}
}
// SecondActivity.java
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.textView);
textView.setText("This is the second activity.");
}
}
Passing Data
When using intents, you can also pass data along. Let’s examine how to pass data via intents through
the following example.
// MainActivity.java (Adding data transfer)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("message", "Hello, Second Activity");
startActivity(intent);
});
}
}
// SecondActivity.java (Receiving data)
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.textView);
String message = getIntent().getStringExtra("message");
textView.setText(message);
}
}
Conclusion
Today we learned about activities and their lifecycle in Android app development,
transitions between activities via intents, and how to pass data.
These concepts are fundamental to Android app development and will serve as a starting point
for creating more complex apps.
In future lessons, we will cover more features and UI elements, so please stay tuned.