APPMOB03
APPMOB03
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.
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:
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.
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.
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:
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:
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: