Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Basic Prerequisites of Learning Android

App Development
Lec:5
onCreate()
• Called when your activity is first created.
• This is the place you normally create your views, open any persistent
data files your activity needs to use, and in general initialize your
activity.
• When calling onCreate, the Android framework is passed a Bundle
object that contains any activity state saved from when the activity
ran before.
The onCreate() method is a fundamental part of Android app development.
It is called when the activity is first created. Here, you initialize essential components of the activity, such as UI elements
and data structures.

This method also receives a Bundle object, which contains the activity's previously saved state, allowing you to restore any
necessary information after the activity has been destroyed and recreated, such as during a screen rotation.

Purpose of Bundle: ---Android Bundles are generally used for passing data from one activity to another.
For example, searching for a location on the browser and witnessing a direct jump
into Google Maps or receiving payment links in Messages Application (SMS) and
on clicking jumping to PayPal or GPay (Google Pay).
--Bundles are used with intent
Intents are used in Android to pass to the data from one
activity to another.
The setContentView(R.layout.activity_main) statement is used in Android development within the onCreate()
method of an activity.

It sets the layout file activity_main.xml as the content view for the activity, defining the user interface (UI) elements
and their arrangement on the screen.

This method call links the Java code (activity) with the XML layout file, allowing the activity to display the defined
UI to the user when launched.

In essence, it inflates the layout resource and associates it with the current activity, enabling interaction with the UI
elements defined in the layout file.
onStart()
• Called just before your activity becomes visible on the screen.
• Once onStart completes, if your activity can become the foreground
activity on the screen, control will transfer to onResume.
• If the activity cannot become the foreground activity for some reason,
control transfers to the onStop method.
A Toast is a feedback message.

It takes a very little space for displaying while the overall activity is interactive and visible to the user. It
disappears after a few seconds.

It disappears automatically. If the user wants a permanently visible message, a Notification can be used.
Another type of Toast is custom Toast, in which images can be used instead of a simple message.
onResume
• Called right after onStart if your activity is the foreground activity on the
screen.
• At this point your activity is running and interacting with the user. You
are receiving keyboard and touch inputs, and the screen is displaying
your user interface.
• onResume is also called if your activity loses the foreground to another
activity, and that activity eventually exits, popping your activity back to
the foreground.
• This is where your activity would start (or resume) doing things that are
needed to update the user interface (receiving location updates or
running an animation, for example).
The onResume() method in Android is part of the activity
lifecycle and is called when the activity is about to start
interacting with the user.

This method is called after onStart() and is typically used to


perform tasks that should occur when the activity becomes
visible and active to the user.

It is also a good place to start animations, acquire exclusive


resources, or refresh the user interface.

Here's a brief explanation of the onResume() method:

onResume() is called when the activity is about to start


interacting with the user.
It is called after the activity has been stopped, but before it
starts interacting with the user.
This method is a good place to perform tasks such as
refreshing the UI, starting animations, or acquiring
resources needed for the activity to function properly.
It is important to keep the onResume() method lightweight
and avoid performing long-running operations that could
slow down the UI responsiveness.
onPause
• onPause() is a method in Android that is called when your activity is about to lose
foreground focus.
• Your activity will no longer have access to the screen during onPause(), so tasks consuming
battery and CPU cycles should be stopped.
• If you're running animations, they won't be visible, so it's advisable to suspend them until
the screen is visible again.
• Use onPause() to store any necessary state that your activity may need when it gains
foreground focus again.
• It's not guaranteed that your activity will resume, so be prepared for the possibility that it
may not.
• If the device runs out of memory, your activity may be killed to make room for system
processes.
• After onPause(), Android may kill your activity without returning control to you
The onPause() method in Android is a callback method that's
invoked when the activity is no longer in the foreground and is
about to be paused or stopped. This typically happens when
another activity comes into the foreground or when the current
activity is partially obscured by another activity's dialog.

The onPause() method is a crucial point for the activity to pause


ongoing processes that are not necessary while the activity is not
visible to the user. For example, animations or background
computations that are consuming resources can be paused in this
method to optimize the performance of the app and conserve
battery life.
onStop
• Called when your activity is no longer visible, either because another
activity has taken the foreground or because your activity is being
destroyed.
The onStop() method in Android is a callback method that's
invoked when the activity is no longer visible to the user. This
can happen when the activity is being stopped or when a new
activity is starting and is covering the entire screen.

The onStop() method is typically used to perform operations


that are needed to pause or release resources that are no
longer required once the activity is no longer visible. This can
include stopping animations, releasing system resources, or
unregistering broadcast receivers.
onDestroy

• The onDestroy() method marks the final phase in the lifecycle of an activity in
Android.
• It serves as the last chance for the activity to perform any necessary tasks before
being terminated.
• This method is typically called when the activity is finished explicitly by calling its
finish() method.
• Android may invoke onDestroy() to reclaim resources if the system determines that
the activity is consuming excessive resources.
• Once onDestroy() is executed, the activity can be destroyed by the system at any
time, without further interaction with the user.
• Activities should use onDestroy() to release any resources, unregister listeners, or
perform cleanup tasks to ensure the efficient use of system resources.
The onDestroy() method in Android is a callback method that's
invoked when the activity is being destroyed and is no longer
accessible to the user. This method is the final opportunity for
the activity to release any resources or perform cleanup
operations before it is removed from memory.

The onDestroy() method is typically used to perform final


cleanup tasks, such as releasing resources that were allocated
during the activity's lifecycle, unregistering listeners, or saving
final state information.
Activity State Changes In Android with Example
Android | Running your first Android app
• After successfully Setting up an Android project, all of the default files
are created with default code in them. Let us look at this default code
and files and try to run the default app created.The panel on the left
side of the android studio window has all the files that the app
includes. Under the java folder, observe the first folder containing the
java file of your project.
• For every activity, a “.java” file and a “.xml” file is created. In this case
for MainActivity, “MainActivity.java” and “activity_main.xml” are
created. The above java file shows us the default code that is present
when an app is created. An activity is created that extends
AppCompactActivity class.
The “res” folder contains “layout” subfolder, which includes the xml files of the
projects.
• You can find the activity_main.xml file under the layout folder. This
the XML file corresponding to the MainActivity. There is an onCreate
function that overrides a function of AppCompactActivity class.
onCreate(Bundle) is where you initialize your activity. When the
activity is first started, then both onCreate() methods are called. But
after the first start of Activity, the onCreate() of application will not be
called for subsequent runs.
• Now, consider the activity_main.xml file, it contains various tags similar
to HTML. The first tag ensures the version. The second tag is usually the
Layout tag. There are various types of Layouts but for now, let us go with
the default RelativeLayout. This is a layout that places the widgets
relative to screen size. There is a TextView widget by default. This
“TextView” is basically the Text field that displays the text specified. It
has various attributes. For now, consider the default attributes present.
The layout_width and layout_height are the width and height of the
widget occupied in the screen. The attribute “wrap_content” refers to
width or height being restricted to the content of the text. The text
attribute takes a string in quotations ( i.e., “ ” ). The content within this is
displayed on the screen.
• Now, click the
“Run” option at the
Toolbar at the top.
You can observe the
option being
highlighted in the
image below.
• You would get a
pop-up as in the
image below
• You can either choose
the emulator or you can
connect your phone
and find them listed
under Connected
Devices but for this you
must enable the
developer options in
your phone and set the
USB debugging mode
on.
• Once done, click on OK.
• Usually, the emulator consumes a lot of RAM. The more RAM
size you have, the faster your emulator will work. Generally, 4GB
is the descent RAM size. Size more than that would increase the
performance of your emulator. The image below shows the
working of the first app, My Application. You can find all the
basic functionalities that your phone has, on the emulator, like
Home button, back button, power, etc.

You might also like