Professional Documents
Culture Documents
Activities in Mobile App Development
Activities in Mobile App Development
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.
• 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.