0% found this document useful (0 votes)
28 views

Android LifeCycle notes

The document outlines key Android programming concepts including Activities, Services, Content Providers, and Broadcast Receivers, each serving distinct roles in app development. It details their definitions, lifecycle methods, and provides examples for better understanding. Mastery of these components and their lifecycles is essential for creating efficient Android applications.

Uploaded by

Teddy yemu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Android LifeCycle notes

The document outlines key Android programming concepts including Activities, Services, Content Providers, and Broadcast Receivers, each serving distinct roles in app development. It details their definitions, lifecycle methods, and provides examples for better understanding. Mastery of these components and their lifecycles is essential for creating efficient Android applications.

Uploaded by

Teddy yemu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Android Programming Concepts: Class Notes

1. Activities

Definition:

An Activity represents a single screen with a user interface (UI). It acts as the entry point for
user interactions with your app. Activities are critical for designing and managing the UI
and handling user interactions.

Key Features:

• Each activity is independent but can communicate with other activities using
Intents.
• They are part of the back stack, which maintains the order of activities.

Lifecycle:

Activities have a well-defined lifecycle to manage their behavior during transitions:

1. onCreate(): Called when the activity is first created. Use this to initialize UI
components.
2. onStart(): Called when the activity becomes visible to the user.
3. onResume(): Called when the activity is ready for the user to interact with.
4. onPause(): Called when the activity is partially obscured (e.g., another activity
appears as a dialog).
5. onStop(): Called when the activity is no longer visible.
6. onDestroy(): Called before the activity is destroyed and removed from memory.

Example:

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize components
}
}
2. Services

Definition:

A Service is a component that performs long-running operations in the background without


providing a user interface. Services continue to run even if the user switches to another
application.

Types of Services:

1. Foreground Service: Runs in the foreground and displays a notification to inform


users (e.g., music players).
2. Background Service: Runs silently in the background (e.g., fetching data).
3. Bound Service: Allows other components to bind to it and communicate with it.

Lifecycle Methods:

• onCreate(): Called when the service is created.


• onStartCommand(): Called when a service is started using startService().
• onBind(): Called when a component binds to the service using bindService().
• onDestroy(): Called when the service is terminated.

Example:

class MyService : Service() {


override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// Perform long-running tasks
return START_STICKY
}

override fun onDestroy() {


super.onDestroy()
// Cleanup tasks
}

override fun onBind(intent: Intent?): IBinder? {


return null
}
}

3. Content Providers

Definition:

Content Providers manage access to a structured set of data. They allow different
applications to share data securely and efficiently.

Key Methods:

• query(): Retrieves data from the content provider.


• insert(): Adds new data.
• update(): Modifies existing data.
• delete(): Removes data.

Use Cases:

• Sharing app-specific data with other apps.


• Centralized access to SQLite databases or other data sources.

Example:

Accessing contacts using the Content Provider:

val cursor = contentResolver.query(


ContactsContract.Contacts.CONTENT_URI,
null, null, null, null
)

Common Content URIs:

• Contacts: ContactsContract.Contacts.CONTENT_URI
• Media: MediaStore.Images.Media.EXTERNAL_CONTENT_URI
4. Broadcast Receivers

Definition:

A Broadcast Receiver is a component that listens for and responds to broadcast messages
from the system or other applications.

Use Cases:

• Responding to system events such as battery level changes, Wi-Fi status, or device
boot.
• Implementing custom application-level events.

Types:

1. Static Receivers: Declared in the AndroidManifest.xml and always active.


2. Dynamic Receivers: Registered programmatically at runtime.

Key Method:

• onReceive(Context context, Intent intent): Called when the broadcast is received.

Example:

class MyBroadcastReceiver : BroadcastReceiver() {


override fun onReceive(context: Context, intent: Intent) {
// Respond to the broadcast
}
}

Registering a Broadcast Receiver:

• Static Registration:

<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

• Dynamic Registration:

val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)


registerReceiver(MyBroadcastReceiver(), filter)

Summary

These components form the backbone of Android application development. Mastering


their use and lifecycle is crucial for building responsive and efficient Android apps. Always
manage their lifecycle carefully to avoid memory leaks or performance issues.

You might also like