0% found this document useful (0 votes)
20 views16 pages

Understanding Android Activity Lifecycle

The document explains the Activity Lifecycle in Android, detailing the various states an Activity can be in, such as created, started, resumed, paused, stopped, restarted, and destroyed. Each state is associated with specific lifecycle methods like onCreate(), onStart(), and onDestroy(), which can be overridden based on application requirements. Understanding these lifecycle methods is crucial for managing an Activity's behavior and user interaction effectively.

Uploaded by

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

Understanding Android Activity Lifecycle

The document explains the Activity Lifecycle in Android, detailing the various states an Activity can be in, such as created, started, resumed, paused, stopped, restarted, and destroyed. Each state is associated with specific lifecycle methods like onCreate(), onStart(), and onDestroy(), which can be overridden based on application requirements. Understanding these lifecycle methods is crucial for managing an Activity's behavior and user interaction effectively.

Uploaded by

moeezshehzad26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Activity Lifecycle With

Example In Android
Said Muhammad
GSSC Peshawar
Activity Lifecycle
Said With Example In Android
Muhammad
GSSC Peshawar
Activity Lifecycle

 Activity is one of the building blocks of Android OS.


 In simple words Activity is a screen that user interact with.
 Every Activity in android has lifecycle like created, started,
resumed, paused, stopped or destroyed. These different
states are known as Activity Lifecycle. In other words we can
say Activity is a class pre-written in Java Programming.
Below is Activity Lifecycle Table
Short description of Activity Lifecycle example:
 onCreate() – Called when the activity is first created
 onStart() – Called just after it’s creation or by restart method
after onStop(). Here Activity start becoming visible to user
 onResume() – Called when Activity is visible to user and
user can interact with it
 onPause() – Called when Activity content is not visible
because user resume previous activity
 onStop() – Called when activity is not visible to user
because some other activity takes place of it
 onRestart() – Called when user comes on screen or
resume the activity which was stopped
 onDestroy()– Called when Activity is not in background
Activity Lifecycle Diagram Shows Different
States
Different Types of Activity Lifecycle
States
 Activity have different states or it’s known as Activity life cycle.
 All life cycle methods aren’t required to override but it’s quite
important to understand them.
 Lifecycles methods can be overridden according to requirements.
 Activity Created: onCreate(Bundle savedInstanceState):
 onCreate() method is called when activity gets memory in the OS. To use
create state we need to override onCreate(Bundle
savedInstanceState) method.
 Bundle is a data repository object that can store any kind of primitive data
and this object will be null until some data isn’t saved in that.
 When an Activity first call or launched then onCreate(Bundle
savedInstanceState) method is responsible to create the activity.
Cont…
 Activity Started: onStart():
 onStart() method is called just after it’s creation.
 In other case Activity can also be started by calling restart method i.e
after activity stop.
 onStart() gets called by Android OS when user switch between
applications. For example, if a user was using Application A and then a
notification comes and user clicked on notification and moved to
Application B, in this case Application A will be paused. And again if a
user again click on app icon of Application A then Application A which
was stopped will again gets started.
 Activity Resumed:onResume():
 Activity resumed is that situation when it is actually visible to user
means the data displayed in the activity is visible to user. In lifecycle it
always gets called after activity start and in most use case after activity
paused (onPause).
Cont…
 Activity Paused: onPause():
 Activity is called paused when it’s content is not visible to user, in
most case onPause() method called by Android OS when user
press Home button (Center Button on Device) to make hide.
 Activity also gets paused before stop called in case user press
the back navigation button. The activity will go in paused state for
these reasons also if a notification or some other dialog is
overlaying any part (top or bottom) of the activity (screen).
 Similarly, if the other screen or dialog is transparent then user
can see the screen but cannot interact with it.
 For example, if a call or notification comes in, the user will get the
opportunity to take the call or ignore it.
Cont…
 Activity Stopped: onStop():
 Activity is called stopped when it’s not visible to user.
 Any activity gets stopped in case some other activity takes place of it.
 For example, if a user was on screen 1 and click on some button and moves to
screen 2. In this case Activity displaying content for screen 1 will be stopped.
 Every activity gets stopped before destroy in case of when user press
back navigation button. So Activity will be in stopped state when hidden
or replaced by other activities that have been launched or switched by
user. In this case application will not present anything useful to the user
directly as it’s going to stop.
 Activity Restarted: onRestart():
 Activity is called in restart state after stop state. So activity’s onRestart()
method gets called when user comes on screen or resume the activity
which was stopped.
 In other words, when Operating System starts the activity for the first
time onRestart() never gets called. It gets called only in case when
activity is resumes after stopped state.
Cont…
 Activity Destroyed: onDestroy():
 Any activity is known as in destroyed state when it’s not in
background. There can different cases at what time activity get
destroyed.
 First is if user pressed the back navigation button then activity will be
destroyed after completing the lifecycle of pause and stop.
 In case if user press the home button and app moves to background.
User is not using it no more and it’s being shown in recent apps list.
So in this case if system required resources need to use somewhere
else then OS can destroy the Activity.
 After the Activity is destroyed if user again click the app icon, in this
case activity will be recreated and follow the same lifecycle again.
Another use case is with Splash Screens if there is call to finish()
method from onCreate() of an activity then OS can directly call
onDestroy() with calling onPause() and onStop().
Practical
Example(activity_main.xml)
Practical
Example([Link])
Practical
Example([Link])
Output
Output
Output

Common questions

Powered by AI

The 'onStart()' state brings an activity into view but does not yet enable user interaction, while the 'onResume()' state indicates the activity is fully in the foreground and can interact with the user. This separation is crucial for performance, as 'onStart()' sets up background constructs and allocates resources necessary for drawing the UI, and 'onResume()' ensures that any setup that directly affects user interaction or responsiveness is executed. This tiered approach allows applications to efficiently manage resources and responsiveness, providing a seamless transition between visibility and interaction .

The 'onPause()' method in an Android application manages short-lived interruptions by pausing the activity and making its content not visible to the user. This happens when the user moves away from the activity via pressing the Home button, receiving a call, or if a non-fullscreen dialog pops up. Additionally, 'onPause()' can be triggered when a transparent activity is displayed on top. The method allows the application to save transient data or state changes that might be needed once the user comes back, ensuring a seamless user experience .

An Android activity transitions to the 'onDestroy()' state when the user completely exits the activity via the back button, explicitly calls the 'finish()' method, or when the system needs to recover resources in low-memory situations. This transition implies that the activity is being completely removed from memory, necessitating the recreation of the activity if the user returns, which can affect user experience if states are not properly saved and restored .

The 'onCreate()' method initializes an activity when it is first instantiated. It is crucial as it sets up the initial state of the activity by loading the UI layout, initializing components, and restoring any previously saved state through the provided 'Bundle' object. This setup ensures the activity is ready for immediate interaction and visual display as it transitions to the 'onStart()' state. The method's significance lies in its role as the foundational setup point for the activity lifecycle .

The Android activity lifecycle manages visibility and user interaction through a series of method calls that help track the activity's state transitions. When an activity is created, the 'onCreate()' method is called to initialize the activity. As it becomes visible to the user, 'onStart()' is called, followed by 'onResume()' when it is ready for user interaction. If a different activity takes precedence, the current activity’s 'onPause()' method is invoked, making it possible for activities to save temporary state. Should the activity no longer be visible, 'onStop()' is called. If the user resumes the paused activity, 'onRestart()' is invoked before 'onStart()'. Finally, 'onDestroy()' is called when the activity is finally removed from memory, typically after the user navigates away and the back stack is cleared .

The 'onStop()' method is triggered when an activity is no longer visible to the user, typically because another activity has been launched and covers it. Situations include transitioning to a new screen or when the activity is replaced by another. After 'onStop()', the activity may either transition to 'onDestroy()' if it is being finished by the system, or to 'onRestart()' before the activity becomes visible again if the user navigates back .

When utilizing the 'onDestroy()' method, developers should focus on releasing all resources, unregistering listeners or receivers, saving any important data, and disentangling memory-intensive objects to avoid memory leaks. Considering 'onDestroy()' can be triggered unexpectedly by the system to reclaim resources, developers should ensure any non-persistent activity state is saved prior to this point. Proper use of this method helps maintain application stability and efficiency, preventing performance degradation or crashes .

The 'onResume()' method facilitates user interaction by marking the point just before the activity comes to the foreground and starts interacting with the user. It signifies that the activity is now at the top of the activity stack and ready for user input, thereby allowing the application to refresh the UI, resume animations, or re-enable listeners that were paused in 'onPause()'. Thus, 'onResume()' is vital for restoring the interactive state of the application .

The 'onRestart()' method is not called when an activity is launched for the first time, as the initial creation is handled by 'onCreate()'. 'onRestart()' is specifically invoked when the activity returns to the foreground from a stopped state, rather than being destroyed. This means 'onRestart()' gets called when the user navigates back to the activity after it has been stopped but not destroyed, allowing the activity to transition back to being visible and interactive .

Handling the 'Bundle' object in the 'onCreate()' method is crucial for maintaining state across application restarts, such as device rotation or when the activity is recreated after being destroyed. The 'Bundle' allows developers to save instance-specific data, which can be restored, ensuring continuity in the user experience. Neglecting this can lead to loss of data and a disjointed user experience as any non-persistent state would be lost, forcing users to restart tasks or interactions .

You might also like