0% found this document useful (0 votes)
10 views9 pages

Module 2

This document covers the operation of Android Virtual Devices (AVDs) and the lifecycle of activities in Android applications. It explains how to create, manage, and run AVDs, as well as the various states of an activity and how to link activities using intents. Additionally, it details how to pass data between activities using intents, including both implicit and explicit intents.

Uploaded by

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

Module 2

This document covers the operation of Android Virtual Devices (AVDs) and the lifecycle of activities in Android applications. It explains how to create, manage, and run AVDs, as well as the various states of an activity and how to link activities using intents. Additionally, it details how to pass data between activities using intents, including both implicit and explicit intents.

Uploaded by

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

MODULE 2

Sample programs- Operation of Android Virtual device - activity in android –


Life cycle of an activity intent – linking activities using intent- data passing
between activities using intent - android components: activities, services,
broadcast receivers, content providers

1. Operations of Android Virtual device


1.a. Create and manage Virtual device
An Android Virtual Device (AVD) is a configuration that defines the characteristics of
an Android phone, tablet, Wear OS, Android TV, or Automotive OS device that you
want to simulate in the Android Emulator. The Device Manager is a tool you can
launch from Android Studio that helps you create and manage AVDs.
To open the new Device Manager, do one of the following:
 From the Android Studio Welcome screen, select More Actions > Virtual Device
Manager.
 After opening a project, select View > Tool Windows > Device Manager from the
main menu bar, then click the +, and then click Create Virtual Device.
 After creating your devices, you will be able to see a list of all the devices on the
device manager panel.
Create an AVD
To create a new AVD:
1. Open the Device Manager.
2. Click Create Device.
The Select Hardware window appears.
3. Select a hardware profile, then click Next.
If you don't see the hardware profile you want, you can create or import a hardware
profile, as described in other sections on this page. The System Image window appears.
4. Select the system image for a particular API level, and then click Next.
The Verify Configuration window appears.
5. Change the AVD properties as needed, and then click Finish.
6. Click Show Advanced Settings to show more settings, such as the skin.
The new AVD appears in the Virtual tab of the Device Manager and the target device
menu.
1.b Edit existing AVDs
You can perform the following operations on an AVD from the Device
Manager's Virtual tab:

 To edit an AVD, click Edit this AVD and make your changes.
 To delete an AVD, click Menu and select Delete.
 To show the associated AVD INI and IMG files on disk, click Menu and
select Show on Disk.
 To view AVD configuration details that you can include in bug reports to the Android
Studio team, click Menu and select View Details.
1.c Run and stop an emulator and clear data
From the Virtual tab, you can perform the following operations on an emulator:

 To run an emulator that uses an AVD, click Launch .

 To stop a running emulator, click Menu and select Stop.

 To clear the data for an emulator, click Menu and select Wipe Data.
2. Activity in android
An Activity represents a single, distinct screen in your Android application. For example, a
login screen, a settings screen, or a map display would each typically be implemented as a
separate Activity.
The Activity class is a crucial component of an Android app, and the way activities are
launched and put together is a fundamental part of the platform's application model. Unlike
programming paradigms in which apps are launched with a main() method, the Android
system initiates code in an Activity instance by invoking specific callback methods that
correspond to specific stages of its lifecycle.
The Activity class is designed to facilitate this paradigm. When one app invokes another, the
calling app invokes an activity in the other app, rather than the app as an atomic whole. In
this way, the activity serves as the entry point for an app's interaction with the user. You
implement an activity as a subclass of the Activity class.
Managing the activity lifecycle
Over the course of its lifetime, an activity goes through a number of states. You use a series
of callbacks to handle transitions between states. The following sections introduce these
callbacks.

onCreate()
You must implement this callback, which fires when the system creates your activity. Your
implementation should initialize the essential components of your activity: For example, your
app should create views and bind data to lists here. Most importantly, this is where you must
call setContentView() to define the layout for the activity's user interface.
When onCreate() finishes, the next callback is always onStart().
onStart()
As onCreate() exits, the activity enters the Started state, and the activity becomes visible to
the user. This callback contains what amounts to the activity’s final preparations for coming
to the foreground and becoming interactive.
onResume()
The system invokes this callback just before the activity starts interacting with the user. At
this point, the activity is at the top of the activity stack, and captures all user input. Most of an
app’s core functionality is implemented in the onResume() method.
The onPause() callback always follows onResume().
onPause()
The system calls onPause() when the activity loses focus and enters a Paused state. This state
occurs when, for example, the user taps the Back or Recents button. When the system
calls onPause() for your activity, it technically means your activity is still partially visible, but
most often is an indication that the user is leaving the activity, and the activity will soon enter
the Stopped or Resumed state.
An activity in the Paused state may continue to update the UI if the user is expecting the UI
to update. Examples of such an activity include one showing a navigation map screen or a
media player playing. Even if such activities lose focus, the user expects their UI to continue
updating.
You should not use onPause() to save application or user data, make network calls, or execute
database transactions. For information about saving data, see Saving and restoring activity
state.
Once onPause() finishes executing, the next callback is either onStop() or onResume(),
depending on what happens after the activity enters the Paused state.

onStop()
The system calls onStop() when the activity is no longer visible to the user. This may happen
because the activity is being destroyed, a new activity is starting, or an existing activity is
entering a Resumed state and is covering the stopped activity. In all of these cases, the
stopped activity is no longer visible at all.
The next callback that the system calls is either onRestart(), if the activity is coming back to
interact with the user, or by onDestroy() if this activity is completely terminating.

onRestart()
The system invokes this callback when an activity in the Stopped state is about to
restart. onRestart() restores the state of the activity from the time that it was stopped.
This callback is always followed by onStart().
onDestroy()
The system invokes this callback before an activity is destroyed.
This callback is the final one that the activity receives. onDestroy() is usually implemented to
ensure that all of an activity’s resources are released when the activity, or the process
containing it, is destroyed.
3. Life cycle of an activity intent
An Activity in Android goes through various states during its lifetime.
These states are managed by the Android system, and developers can use callback methods to
respond to these state changes.

 onCreate():
 Called when the Activity is first created.
 This is where you initialize your UI, variables, and other setup tasks.
 onStart():
 Called when the Activity becomes visible to the user.
 onResume():
 Called when the Activity is in the foreground and the user can interact
with it.
 onPause():
 Called when the Activity is about to lose focus.
 This is where you should save any data that needs to be persisted.
 onStop():
 Called when the Activity is no longer visible to the user.
 onDestroy():
 Called before the Activity is destroyed.
 onRestart():
 Called when a stopped activity is about to be restarted.

 Starting an Activity:
 When you use an Intent to start a new Activity, the current Activity may
go through onPause() and onStop() states.
 The new Activity will then go through its onCreate(), onStart(), and
onResume() states.
 Returning to an Activity:
 When the user finishes the new Activity and returns to the previous one,
the previous Activity will go through onRestart(), onStart(), and
onResume() states.
 Intents and Data:
 Intents can carry data between Activities. This data can be used to
initialize the new Activity or to pass results back to the previous Activity.
 Activity Destruction:
 If the system needs to reclaim memory, it may destroy Activities that are in the
background (in the onStop() state). When the user returns to these Activities, they will
be recreated from scratch, going through onCreate() again.

4. Linking activities using intent


An Intent is a messaging object you can use to request an action from
another app component. Although intents facilitate communication between
components in several ways, there are three methods are their:
 Context.startActivity()
An Activity represents a single screen in an app. You can start a new
instance of an Activity by passing an Intent to startActivity().
The Intent describes the activity to start and carries any necessary data.
 Context.startService()
A Service is a component that performs operations in the background without a
user interface.
 Context.sendBroadcast()
This is to deliver the message to broadcast receivers.
TYPES OF INTENTS
1. Implicit Intent
2. Explicit Intent
Implicit Intent -It do not name a specific component, but instead declare a general action to
perform, which allows a component from another app to handle it.
For example, if you want to show the user a location on a map, you can use an implicit intent
to request that another capable app show a specified location on a map.
Step by Step Implementation
To create a new project in Android Studio Create/Start a New Project in Android Studio .
Creating an Android App to Open a Webpage Using Implicit Intent
Step 1: Create a New Project in Android Studio
Create an XML file and Java File
Step 2: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the
code for the activity_main.xml file.
Step 3: Working with the MainActivity File
Now, we will create the Backend of the App. For this, Open the MainActivity file and
instantiate the component (Button) created in the XML file using the findViewById() method.
This method binds the created object to the UI Components with the help of the assigned ID.

Explicit Intent – It specify which component of which application will satisfy the intent, by
specifying a full Component Name.
You'll typically use an explicit intent to start a component in your own app, because you
know the class name of the activity or service you want to start.
For example, you might start a new activity within your app in response to a user action, or
start a service to download a file in the background.
Step by Step Implementation
How to create an Android App to move to the next activity using Explicit Intent(with
Example)
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio Create/Start a New Project in Android Studio .
The code for that has been given in both Java and Kotlin Programming Language for Android
Step 2: Working with the activity_main.xml File
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the
code for the activity_main.xml file. Comments are added inside the code to understand the
code in more detail.

When you use an implicit intent, the Android system finds the appropriate component to start
by comparing the contents of the intent to the intent filters declared in the manifest file of
other apps on the device. If the intent matches an intent filter, the system starts that
component and delivers it the Intent object. If multiple intent filters are compatible, the
system displays a dialog so the user can pick which app to use.
An intent filter is an expression in an app's manifest file that specifies the type of intents that
the component would like to receive. For instance, by declaring an intent filter for an activity,
you make it possible for other apps to directly start your activity with a certain kind of intent.
Likewise, if you do not declare any intent filters for an activity, then it can be started only
with an explicit intent.

Figure explains How an implicit intent is delivered through the system to start another
activity: [1] Activity A creates an Intent with an action description and passes it
to startActivity(). [2] The Android System searches all apps for an intent filter that matches
the intent. When a match is found, [3] the system starts the matching activity (Activity B) by
invoking its onCreate() method and passing it the Intent.
5. DATA PASSING BETWEEN ACTIVITIES USING INTENT
Through Intent we can move from one activity to another activity and
Intent can also be used to pass the data from one activity to another
activity. In the previous article, we designed the Login Page and now we
will learn how to use Intent to pass data from LoginActivity to the next
activity.

Step 1:
First of all, we have to link the views
of activity_login.xml with LoginActivity In activity_login.xml, we have used
two EditTexts and one button. Let’s initialize them in our LoginActivity.
EditText edittextfullname,edittextusername; : This is the way to define the
views which we have used in the layout and you can give any name you want.
Here we have given edittextfullname and edittextusername.
 Link the views in LoginActivity
edittextfullname=findViewById(R.id.editTextFullName); : This line help to
find the id by findViewById method and the id which we have given for widget
in layout XML file.
Step 2:
Name the new activity as SecondActivity.
Now we have created two Activities. With the help of Intent, we will go from
one activity to another activity (LoginActivity to SecondActivity).
Now it’s time to use Intent but first we have to learn setOnClickListener.
Step 3:
When we press the button, setOnClickListener() will be called and
inside setOnClickListener(), we will write code to open new activity and we
can do this with the help of Intent.
So in the parentheses write “new View.OnClickListener” and
click View.OnClickListener from the pop-up.
Step 4:
If we have to open one activity from another activity we have to add an Intent
code in onClick(View view) method as shown below
Intent intent = new Intent(Source,Destination);startActivity(intent):
Source: It means the current activity in which you are present.
Destination: It means the activity where you have to go.
startActivity(intent); : This starts the activity.
Step 5:
To pass the data through Intent we will use putExtra() method and in
parameter, we will use Key-Value Pair.
Step 6:
Now open the second Activity
Our next task is to print fullName in ActionBar of SecondActivity.
Run the project
The data is successfully passed as you can see. The name written
in FullName is set on on ActionBar as Title.

You might also like