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

APPMOB03

The document provides an overview of Android mobile app activities and resources, explaining that activities represent screens in the app while resources include visual and audio assets. It details the creation and management of activities, including the Activity Lifecycle and methods for handling multiple activities. Additionally, it describes various resource types used in Android development, such as images, layouts, strings, and colors, and how to access them in code.

Uploaded by

achrafmeziani07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

APPMOB03

The document provides an overview of Android mobile app activities and resources, explaining that activities represent screens in the app while resources include visual and audio assets. It details the creation and management of activities, including the Activity Lifecycle and methods for handling multiple activities. Additionally, it describes various resource types used in Android development, such as images, layouts, strings, and colors, and how to access them in code.

Uploaded by

achrafmeziani07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapiter III:

Android mobile app activities and


resources
Introduction to Activities and Resources
In the context of mobile app development,
activities and resources are essential concepts that
form the core of the user interface and application
functionality. Activities represent different screens
or windows in the app, while resources are the
visual and audio assets that populate the app's
interface.
Definition of Activities
In Android, an activity is a component of an app
that represents a single screen with a user interface. It
can contain UI elements such as buttons, text fields,
images, and other interactive widgets. Activities are
designed to handle user interactions, such as button
clicks, and to respond to user input. They are an
important part of the Android app architecture and
provide a way for developers to create dynamic and
interactive user experiences.
Definition of Resources
In Android, resources refer to the files and static
content that an app uses, such as images, layouts, strings,
colors, and styles. Resources are compiled and packaged
within the app, making it easy for developers to access
and use them in their code. They can be accessed and
used in different parts of an app, including the UI, code,
and manifest file, among others. Using resources in an
app ensures consistency, reduces repetition, and
simplifies maintenance.
Creating Activities
To create an Activity in Android Studio, follow these steps:

1.Open your project in Android Studio.

2.Right-click on the app folder in the Project view, then select New -> Activity -> Empty
Activity.

3.In the dialog that appears, enter the name of the Activity in the Activity name field.

4.Choose the layout and menu options you want for the Activity.

5.Click Finish to create the Activity.

After you create the Activity, you can add code to it in the Java file and create the layout in
the XML file.
Creating Activities
To create an Activity in Android Studio, follow these steps:

1.Open your project in Android Studio.

2.Right-click on the app folder in the Project view, then select New -> Activity -> Empty
Activity.

3.In the dialog that appears, enter the name of the Activity in the Activity name field.

4.Choose the layout and menu options you want for the Activity.

5.Click Finish to create the Activity.

After you create the Activity, you can add code to it in the Java file and create the layout in
the XML file.
Activity Lifecycle
The Activity Lifecycle refers to the different states that an activity can be in at any given time while it's running. It's
important for Android developers to understand the Activity Lifecycle in order to manage the behavior of their app as it transitions
between different states.

The Activity Lifecycle is composed of the following methods:


1.onCreate(): This method is called when the activity is first created. It's where you initialize the activity, including
setting up the UI and other components.
2.onStart(): This method is called when the activity becomes visible to the user. It's where you can start animations
or other visual effects.
3.onResume(): This method is called when the activity is ready to interact with the user. It's where you can start or
resume any running processes.
4.onPause(): This method is called when the activity is about to lose focus. It's where you should save any unsaved
data or stop any running processes.
5.onStop(): This method is called when the activity is no longer visible to the user. It's where you can stop any
running processes that are not needed.
6.onRestart(): This method is called when the activity is being restarted from a stopped state.
7.onDestroy(): This method is called when the activity is being destroyed. It's where you should release any
resources that are no longer needed.
By understanding the Activity Lifecycle and the methods that are called during each phase, you can create more
responsive and reliable apps that behave predictably for users.
Activity Lifecycle
Starting
1. onCreate()
2. onStart()
3. onResume()

Running
1. onCreate()
2. onStart() onPause()
3. onResume()
onResume()
Stopped Paused
onStop()

onDestory()

Destoryed
Activity Class Example
Managing multiple Activities
Managing multiple activities refers to how the app handles the different states of various activities in
the application, such as creating, pausing, resuming, and destroying. The app can navigate between different
activities using an intent, which starts a new activity and passes the control to the new activity.

To manage multiple activities, the app developer needs to use different methods provided by the
Android OS, such as startActivity(), finish(), and onActivityResult(), etc. The startActivity() method starts a new
activity, the finish() method is used to finish the current activity, and the onActivityResult() method is called
when the activity returns a result.

The application can keep track of the different states of each activity by implementing the Activity
class's various callback methods, which include onCreate(), onStart(), onResume(), onPause(), onStop(),
onRestart(), and onDestroy(). The app can use these methods to save the activity's state when it is paused or
destroyed and to restore it when it is resumed.

To manage multiple activities efficiently, the app developer needs to design the app in such a way that it uses
resources efficiently, such as memory and CPU usage, and minimizes the number of activities to reduce the
overhead of starting and stopping activities.
Managing multiple Activities
To manage multiple activities in an Android app, you can use the following
approaches:

Start an Activity: You can start a new activity by creating an Intent and
calling the startActivity() method. This will create a new instance of the activity and
add it to the top of the activity stack.
Finish an Activity: When you no longer need an activity, you can call the
finish() method to remove it from the activity stack.
Pass data between Activities: You can pass data between activities by
adding extras to the intent that starts the new activity, or by using a Bundle object.
Launch modes: You can use launch modes to control how new activities are
launched and how they interact with existing activities in the activity stack.
Back stack: You can manage the back stack of activities by setting flags on
the intent that starts the new activity, or by using the TaskStackBuilder class to
build an artificial back stack.
Definition of an Activity in the AndroidManifest.xml file.
In the AndroidManifest.xml file, the activity is defined as an <activity> tag that contains
information about the activity, such as its class name, the application label, icon, required
permission, intent filters, etc. This tag is placed in the root <application> element.
Here's an example of an <activity> tag in the AndroidManifest.xml file:

In this example, the activity is named "MainActivity" and is defined with a label and an icon. It
also requires the "ACCESS_FINE_LOCATION" permission. Additionally, the <intent-filter> tag specifies that
this activity is the main (launcher) activity of the application, which will be launched when the user clicks
on the application icon on the home screen.
Definition of an Activity in the AndroidManifest.xml file.
Here's an example of an Android application with a single activity that
displays the message "Hello" in a Toast:
In the MainActivity.java file, add the following code:

In this example, the MainActivity activity is defined in the


AndroidManifest.xml file as the main (launcher) activity of the application.
When the application is launched, the activity is created and the message
"Hello" is displayed in a Toast.
Resources
In Android app development, resources refer to elements such as images,
layout files, string files, styles, colors, dimensions, and other files used to build the
user interface and app features.
These resources are organized by type and language configuration and can
be easily referenced from the source code. They are often used in conjunction with
XML layouts to define the appearance and behavior of UI components. Resources
are important for creating user-friendly and customizable apps for different
configurations.
Resources
Here is a table that lists some of the common types of resources used
in Android app development:
Resource Type Description
Images PNG, JPEG, and GIF files used to display pictures, logos, icons, and other visual elements within the app.
XML files that define the structure and appearance of the user interface, including the placement of UI
Layouts
components such as buttons, text fields, and images.
Text strings used within the app, such as menu items, button labels, and error messages, can be stored
Strings
in a string resource file.
Styles A style resource can be defined to apply a consistent look and feel to UI elements throughout the app.
A color resource can be defined to store color values that can be applied to UI elements such as
Colors
backgrounds, text, and borders.
A dimension resource can be defined to store sizes such as height, width, and margins that can be used
Dimensions
in the layout files.
Drawables Resources that define graphic images and animation.
Animations Resources that define how to perform tweened animations.
Menus Resource files that define app menus and options.
Values Resources that define general values, such as integers, booleans, and arrays.

This is not an exhaustive list, but it covers some of the most commonly used resource
types in Android app development.
Resources
Here's an example of how you might use some of these resource types in an Android app:

Images: You can include an image in your app by adding it to the


"drawable" folder in your app's resources. For example, if you have an image
file called "my_image.png", you can reference it in your layout XML file like
this:
Resources
Strings: You can define a string resource in the "strings.xml" file in
your app's resources. For example:

You can then reference this string in your layout XML file or Java
code like this:
Resources
Colors: You can define a color resource in the "colors.xml" file in your
app's resources. For example:

You can then reference this color in your layout XML file or Java
code like this:
Resources
Access resources in Java code:

Strings: You can access a string resource in Java code using the getString()
method of the Resources class. For example:

Colors: You can access a color resource in Java code using the getColor()
method of the Resources class. For example:

Dimensions: You can access a dimension resource in Java code using the
getDimension() method of the Resources class. For example:
Resources
You can access an image resource in Java code using the
getDrawable() method of the Resources class. For example:

This will retrieve the image with the name "my_image" from the "drawable"
folder in your app's resources. Note that the getDrawable() method was
deprecated in API level 22. In newer versions of Android, you should use the
getDrawable() method of the ContextCompat class instead, like this:

This will retrieve the image with the name "my_image" from the "drawable"
folder in your app's resources, and the this parameter should be replaced
with a reference to your Activity or Context. Once you have the Drawable
object, you can use it to set the image of an ImageView or other UI element
in your app.
Resources
You can apply a style to a view or a group of views in Java code using
the setStyle() method of the View class. Here's an example:

This will apply the style with the name "MyTextStyle" to the
myTextView view. The style should be defined in your app's resources, in the
"styles.xml" file. Here's an example of a style definition:

You might also like