Intents, Adapters, and Internet Resources
1. Introduction to Intents in Android
Intents are messaging objects used to request an action from another Android component.
They enable communication between activities, services, and broadcast receivers.
Types of Intents:
1. Explicit Intent – Used to launch a specific activity or service within the same app.
2. Implicit Intent – Used to request an action that can be handled by any app (e.g., open
browser).
Components of an Intent:
• Action – Defines the operation to perform (e.g., ACTION_VIEW).
• Data (URI) – Specifies data such as URLs or file paths.
• Category – Provides additional information about the action.
• Extras – Key-value data sent with the intent.
• Flags – Control activity launch behavior.
Example (Explicit Intent):
Intent intent = new Intent([Link], [Link]);
startActivity(intent);
Example (Implicit Intent):
Intent intent = new Intent(Intent.ACTION_VIEW);
[Link]([Link]("[Link]
startActivity(intent);
2. Benefits of Using Intents
• Modularity – Components are independent and reusable.
• Flexibility – Apps can choose which components perform actions.
• Inter-App Communication – Ability to communicate with other installed apps.
• Task Management – Helps manage activity stack.
• Security – Intent permissions restrict unauthorized access.
3. Introduction to Adapters in Android
Adapters act as a bridge between data sources and UI components like ListView and
RecyclerView.
Types of Adapters:
1. ArrayAdapter – For simple lists.
2. BaseAdapter – For custom list layouts.
3. CursorAdapter – For database-backed views.
4. [Link] – Most efficient modern adapter with ViewHolder pattern.
How Adapters Work:
Data Source → Adapter converts data → UI Component displays items.
RecyclerView Example:
class MyAdapter extends [Link]<MyViewHolder> {
// binds data to UI efficiently using ViewHolder pattern
4. Internet Resources in Android
Android allows apps to communicate with web services, APIs, and online resources.
Uses:
• Fetching JSON data from APIs
• Authentication and cloud communication
• Streaming media
• Uploading/downloading files
Required Permissions ([Link]):
<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link].ACCESS_NETWORK_STATE"/>
Modern Networking Libraries:
• Retrofit – Most popular for REST APIs
• OkHttp – HTTP client
• Volley – Google networking library
• WorkManager – Schedule background network tasks
Retrofit Example:
Retrofit retrofit = new [Link]()
.baseUrl("[Link]
.addConverterFactory([Link]())
.build();
5. Efficient Internet Usage & Best Practices
Battery-friendly Techniques:
• Prefetch larger data batches
• Bundle multiple network calls
• Reuse connections instead of creating new ones
• Use caching to reduce unnecessary network calls
• Avoid refreshing too frequently
• Use WorkManager for background sync
Connectivity Handling:
• Check network availability
• Use try–catch blocks for API failures
• Display user-friendly error messages
6. Cloud Services
Modern cloud services for Android applications:
• Firebase (Realtime Database, Authentication, Firestore)
• AWS (S3, DynamoDB, EC2)
• Google Cloud Platform
• Microsoft Azure
Benefits:
• Scalability
• Secure authentication
• Faster backend development
• Real-time updates
Code Example
1. Explicit Intent Example
Explicit intents specify the target activity class.
// Explicit Intent Example
Intent intent = new Intent([Link], [Link]);
startActivity(intent);
2. Implicit Intent Example
Implicit intents request actions that other apps can handle.
// Implicit Intent Example
Intent intent = new Intent(Intent.ACTION_VIEW);
[Link]([Link]("[Link]
startActivity(intent);
3. Passing Data Between Activities
Use putExtra() and getIntent() to send/receive data.
// Sending Data
Intent intent = new Intent([Link], [Link]);
[Link]("username", "Areeba");
startActivity(intent);
// Receiving Data
String name = getIntent().getStringExtra("username");
4. RecyclerView Adapter Example
RecyclerView uses an Adapter + ViewHolder pattern for efficient lists.
public class MyAdapter extends
[Link]<[Link]> {
private ArrayList<String> dataList;
public MyAdapter(ArrayList<String> dataList) {
[Link] = dataList;
}
public class MyViewHolder extends [Link] {
TextView title;
public MyViewHolder(View itemView) {
super(itemView);
title = [Link]([Link]);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View v = [Link]([Link]())
.inflate([Link].list_item, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
[Link]([Link](position));
}
@Override
public int getItemCount() {
return [Link]();
}
}
MyAdapter connects your data (ArrayList<String>) with the RecyclerView.
ViewHolder (MyViewHolder) holds references to item views
onCreateViewHolder creates the view.
onBindViewHolder binds data to the view.
getItemCount tells RecyclerView how many items to display.
5. Retrofit API Call Example
Retrofit is the recommended 2024+ networking library for Android.
// Retrofit Interface
public interface ApiService {
@GET("users")
Call<List<User>> getUsers();
}
// Retrofit Builder
Retrofit retrofit = new [Link]()
.baseUrl("[Link]
.addConverterFactory([Link]())
.build();
ApiService api = [Link]([Link]);
// API Call
Call<List<User>> call = [Link]();
[Link](new Callback<List<User>>() {
@Override
public void onResponse(Call<List<User>> call, Response<List<User>>
response) {
if ([Link]()) {
List<User> users = [Link]();
}
}
@Override
public void onFailure(Call<List<User>> call, Throwable t) {
Log.e("API Error", [Link]());
}
});
Part Purpose
Interface Defines API endpoints
Creates Retrofit with base URL + JSON
Retrofit Builder
converter
create() Makes object from interface
getUsers() Prepares API call
Part Purpose
enqueue() Executes call in background
onResponse() API succeeded
onFailure() API failed
6. JSON Parsing Example ([Link])
Android supports JSON parsing using JSONObject.
String json = "{ "name": "Areeba", "age": 21 }";
JSONObject obj = new JSONObject(json);
String name = [Link]("name");
int age = [Link]("age");
7. Conclusion
This lecture covered Intents, Adapters, Internet usage, APIs, cloud services, and best
practices.
Understanding these core concepts helps in building modern, scalable Android applications.