0% found this document useful (0 votes)
5 views7 pages

Lecture 6 - First Part

The document provides an overview of activities in Android, defining them as fundamental components that represent user interactions within an app. It details the activity lifecycle, including methods such as onCreate, onStart, onResume, onPause, onStop, onDestroy, and onRestart, and illustrates these concepts using WhatsApp as a real-world example. Additionally, it emphasizes the importance of the AndroidManifest.xml file for registering activities and outlines homework tasks for further exploration and practice.

Uploaded by

Mayur Gole
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)
5 views7 pages

Lecture 6 - First Part

The document provides an overview of activities in Android, defining them as fundamental components that represent user interactions within an app. It details the activity lifecycle, including methods such as onCreate, onStart, onResume, onPause, onStop, onDestroy, and onRestart, and illustrates these concepts using WhatsApp as a real-world example. Additionally, it emphasizes the importance of the AndroidManifest.xml file for registering activities and outlines homework tasks for further exploration and practice.

Uploaded by

Mayur Gole
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/ 7

GODJN Solutions Pvt. Ltd.

Proliferate Gratification!

Day 6: Understanding Activities

1. Introduction to Activities

1.1 What is an Activity?


- Definition: An Activity in Android is a single, focused thing that the user can do. It's one of the
fundamental building blocks of an Android app.
- Functionality: It represents a screen in an app, typically with a user interface.
- An Activity in Android is a crucial component of an app, representing a single, focused task
that users can perform. Each activity typically provides a screen with which users can interact to
do something, such as viewing a map, taking a photo, sending an email, or playing a game.
When an activity starts, it goes through a series of lifecycle states, which determine how it
interacts with the user and how resources are managed.

1.2 Activity Lifecycle


- Lifecycle Overview: Understanding the lifecycle of an activity is crucial for managing the user
experience and ensuring smooth transitions between activities.
- onCreate(): Called when the activity is first created. Initialize your activity here.
- 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 put the activity into the background.
- onStop(): Called when the activity is no longer visible to the user.
- onDestroy(): Called before the activity is destroyed.
- onRestart(): Called after the activity has been stopped, just before it is started again.

WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane contact@godjn.com


Gera’s Imperium Alpha, Kharadi, Pune godjnsolutions@gmail.com
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!

Let's consider WhatsApp, a popular messaging app, to understand how activities work in a real-world
scenario.

Key Activities in WhatsApp:

1. Main Activity (Chats Screen):


o Description: This is the primary screen users see when they open WhatsApp. It
displays a list of all recent chats.

WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane contact@godjn.com


Gera’s Imperium Alpha, Kharadi, Pune godjnsolutions@gmail.com
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!

o Functionality: Users can scroll through their chat history, click on a chat to open
it, start a new chat, or access settings.
2. Chat Activity:
o Description: When a user selects a chat from the main screen, this activity opens,
showing the conversation with a specific contact or group.
o Functionality: Users can send and receive messages, share media (photos,
videos, voice notes), and perform actions like calling the contact or viewing their
profile.
3. Contacts Activity:
o Description: This screen allows users to browse their contacts to start a new
chat.
o Functionality: Users can search for contacts, view contact details, and initiate a
new chat or call.
4. Status Activity:
o Description: This screen displays the status updates of the user's contacts.
o Functionality: Users can view status updates, reply to them, and post their own
status updates.
5. Call Activity:
o Description: This screen shows the call history and allows users to make voice or
video calls.
o Functionality: Users can view recent calls, make new calls, and see missed call
notifications.
6. Settings Activity:
o Description: This screen provides access to app settings.
o Functionality: Users can change their profile picture, update status, configure
privacy settings, manage notifications, and access help.

Activity Lifecycle in WhatsApp:

1. MainActivity (Chats Screen):


o When the user opens WhatsApp, MainActivity is created and started.
o This activity stays active as long as the user is navigating through the chat list.
2. ChatActivity:
o When the user selects a chat, ChatActivity is launched.
o MainActivity may be paused or stopped, depending on navigation.
o The user can send and receive messages, which keeps ChatActivity in the
foreground.
3. Navigating Between Activities:
o Users can navigate back to MainActivity from ChatActivity, which will either
resume or restart, refreshing the chat list if necessary.
o Users can switch to ContactsActivity to start a new chat, or to StatusActivity to
check statuses. Each switch involves pausing or stopping the current activity and
starting a new one.

WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane contact@godjn.com


Gera’s Imperium Alpha, Kharadi, Pune godjnsolutions@gmail.com
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!

2. EXAMPLE

3. Registering Activities in the Android Manifest

3.1 AndroidManifest.xml
- Purpose: The manifest file provides essential information about your app to the Android
system, which the system must have before it can run any of the app's code.
- Declaration Example:
xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane contact@godjn.com


Gera’s Imperium Alpha, Kharadi, Pune godjnsolutions@gmail.com
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!

3.2 Key Elements


- <manifest>: The root element of the manifest file.
- <application>: Encapsulates the application details.
- <activity>: Defines an activity, including its intent filters and meta-data.

4. Activity Lifecycle Methods

4.1 Overriding Lifecycle Methods


- onCreate(): Initialize the activity.
java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

- onStart(): Called when the activity is becoming visible.


java
@Override
protected void onStart() {
super.onStart();
// Code to execute when the activity is becoming visible
}

WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane contact@godjn.com


Gera’s Imperium Alpha, Kharadi, Pune godjnsolutions@gmail.com
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!

- onResume(): Called when the activity will start interacting with the user.
java
@Override
protected void onResume() {
super.onResume();
// Code to execute when the activity starts interacting with the user
}

- onPause(): Called when the system is about to put the activity into the background.
java
@Override
protected void onPause() {
super.onPause();
// Code to execute when the activity is about to go into the background
}

- onStop(): Called when the activity is no longer visible.


java
@Override
protected void onStop() {
super.onStop();
// Code to execute when the activity is no longer visible
}

- onDestroy(): Called before the activity is destroyed.

WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane contact@godjn.com


Gera’s Imperium Alpha, Kharadi, Pune godjnsolutions@gmail.com
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!

java
@Override
protected void onDestroy() {
super.onDestroy();
// Code to execute before the activity is destroyed
}

- onRestart(): Called after the activity has been stopped, just before it is started again.
java
@Override
protected void onRestart() {
super.onRestart();
// Code to execute when the activity is restarted
}

5. Homework
- Research: Explore different scenarios where each lifecycle method is particularly useful.
- Practice: Create an app with multiple activities and experiment with different lifecycle
methods.
- Explore: Modify the Android Manifest file to add additional activities and experiment with
different configurations.

By the end of Day 6, students should have a solid understanding of activities in Android, their
lifecycle, how to create them, and how to manage their states effectively. This foundational
knowledge will be crucial for building more complex applications in the following days.

WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane contact@godjn.com


Gera’s Imperium Alpha, Kharadi, Pune godjnsolutions@gmail.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy