Overview
In Android development, an Activity represents a single screen with a user interface. Activities are fundamental building blocks of an Android app, and understanding how to create and manage them is crucial for any Android developer.
Key Concepts
- Activity Lifecycle: Understanding the different states an Activity can be in and how to manage transitions between these states.
- Creating an Activity: How to define and implement an Activity in your Android project.
- Intents: Mechanisms to start Activities and pass data between them.
- UI Design: Linking XML layouts to Activities to create interactive user interfaces.
Activity Lifecycle
The Activity lifecycle consists of several states, each representing a different stage in the Activity's existence. The primary lifecycle methods are:
onCreate()
: Called when the Activity is first created.onStart()
: Called when the Activity becomes visible to the user.onResume()
: Called when the Activity starts interacting with the user.onPause()
: Called when the system is about to start resuming another Activity.onStop()
: Called when the Activity is no longer visible to the user.onDestroy()
: Called before the Activity is destroyed.
Lifecycle Diagram
State | Description |
---|---|
onCreate |
Initialize the Activity. Set up the UI and other resources. |
onStart |
Make the Activity visible to the user. |
onResume |
Start interacting with the user. |
onPause |
Pause ongoing actions that should not continue while the Activity is not in the foreground. |
onStop |
The Activity is no longer visible. Release resources that are not needed while the Activity is not visible. |
onDestroy |
Clean up any resources including ending threads, closing database connections, etc. |
Creating an Activity
To create a new Activity in Android Studio:
-
Define the Activity in Java/Kotlin:
// MainActivity.java package com.example.myfirstapp; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
// MainActivity.kt package com.example.myfirstapp import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
-
Define the Activity in the Manifest:
<!-- AndroidManifest.xml --> <application ... > <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
Intents
Intents are used to start Activities and pass data between them. There are two types of Intents:
-
Explicit Intents: Directly specify the Activity to start.
Intent intent = new Intent(this, SecondActivity.class); startActivity(intent);
val intent = Intent(this, SecondActivity::class.java) startActivity(intent)
-
Implicit Intents: Do not specify the exact Activity to start. Instead, they declare a general action to perform, which allows other apps to handle the request.
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.example.com")); startActivity(intent);
val intent = Intent(Intent.ACTION_VIEW) intent.data = Uri.parse("http://www.example.com") startActivity(intent)
Practical Example
Let's create a simple app with two Activities. The first Activity will have a button that starts the second Activity.
-
First Activity (MainActivity):
// MainActivity.java package com.example.myfirstapp; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; 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(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } }); } }
// MainActivity.kt package com.example.myfirstapp import android.content.Intent import android.os.Bundle import android.view.View import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val button: Button = findViewById(R.id.button) button.setOnClickListener { val intent = Intent(this, SecondActivity::class.java) startActivity(intent) } } }
-
Second Activity (SecondActivity):
// SecondActivity.java package com.example.myfirstapp; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } }
// SecondActivity.kt package com.example.myfirstapp import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class SecondActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_second) } }
-
XML Layouts:
-
activity_main.xml
:<!-- activity_main.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to Second Activity" /> </RelativeLayout>
-
activity_second.xml
:<!-- activity_second.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SecondActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the Second Activity!" /> </RelativeLayout>
-
Practical Exercise
Task: Create an Android app with two Activities. The first Activity should have a button that, when clicked, starts the second Activity. The second Activity should display a message.
Solution
- Create a new project in Android Studio.
- Define
MainActivity
andSecondActivity
as shown in the examples above. - Create the XML layouts for both Activities.
- Update the
AndroidManifest.xml
to include both Activities.
Common Mistakes
- Forgetting to declare the Activity in the Manifest: Ensure that each Activity is declared in the
AndroidManifest.xml
. - Incorrect Intent usage: Make sure to use the correct context and target Activity class when creating an Intent.
Conclusion
In this lesson, you learned about the fundamental concept of Activities in Android development. You explored the Activity lifecycle, how to create and manage Activities, and how to use Intents to navigate between them. Understanding these concepts is essential for building interactive and user-friendly Android applications. In the next lesson, you will learn about running your app on an emulator.
Android Studio Course
Module 1: Introduction to Android Studio
- Introduction to Android Studio
- Setting Up Android Studio
- Understanding the Android Studio Interface
- Creating Your First Android Project
Module 2: Basic Android Development
- Understanding Android Project Structure
- Introduction to XML Layouts
- Basic UI Components
- Introduction to Activities
- Running Your App on an Emulator
Module 3: Intermediate Android Development
- Introduction to Intents
- Working with Fragments
- Handling User Input
- Using RecyclerView
- Networking in Android
Module 4: Advanced Android Development
- Data Persistence with SQLite
- Using Room for Database Management
- Advanced UI Components
- Custom Views and Canvas
- Working with Background Tasks
Module 5: Professional Android Development
- Implementing MVVM Architecture
- Dependency Injection with Dagger
- Unit Testing and UI Testing
- Publishing Your App on Google Play
- Performance Optimization