6 - Activity Life Cycle

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

ACTIVITY LIFE CYCLE

ANDROID ACTIVITY (STATES)


◾An activity can have five states, which are :
1. Started / Running (Active)
2. Paused
3. Resumed
4. Stopped
5. Destroyed
1. Running (Active) State

• When an Activity is in active state, it means it is active and running.


• It is visible to the user and the user is able to interact with it.
• Android Runtime treats the Activity in this state with the highest
priority and never tries to kill it.
1. Running (Active) State …
2. Paused State

• An activity being in this state means that the user can still see
the Activity in the background such as behind a transparent
window or a dialog box i.e it is partially visible.
• The user cannot interact with the Activity until he/she is done with
the current view.
• Android Runtime usually does not kill an Activity in this state but
may do so in an extreme case of resource crunch.
2. Paused State …
3. Resumed State …

◾ It is when an activity goes from the paused state to the foreground


that is an active state.
4. Stopped State

• When a new Activity is started on top of


the current one or when a user hits the
Home key, the activity is brought to
Stopped state.
• The activity in this state is invisible,
but it is not destroyed.
• Android Runtime may kill such an
Activity in case of resource crunch.
4. Stopped State…
5. Destroy State

• When a user hits a Back key or


Android Runtime decides to reclaim
the memory allocated to an Activity
i.e in the paused or stopped state,
It goes into the Destroyed state.
• The Activity is out of the memory
and it is invisible to the user.
Android Activity Methods
◾ Android activities go through five states during their
entire lifecycle. These activities have callback
methods() to describe each activity in each of the
four stages. These methods need to be overridden by
the implementing subclass. In Android, we have the
following 7 callback methods that activity uses to go
through the four states:
1. onCreate()
2. onStart()
3. onResume()
4. onPause()
5. onStop()
6. onRestart()
7. onDestroy()
onCreate()

◾ The Android oncreate() method is called at the very start when an


activity is created. An activity is created as soon as an application is
opened. This method is used in order to create an Activity.
onStart()
◾ The Android onstart() method is invoked as soon as the activity
becomes visible to the users. This method is to start an activity. The
activity comes on the forescreen of the users when this method is
invoked.
onPause()

◾ The Android onPause() method is invoked when the activity doesn’t receive
any user input and goes on hold. In the pause state, the activity is partially
visible to the user. This is done when the user presses the back or home
buttons. Once an activity is in the pause state, it can be followed by either
onResume() or onStopped() callback method.
onRestart()
◾ The Android onRestart() method is invoked when activity is about to start
from the stop state. This method is to restart an activity that had been
active some time back. When an activity restarts, it starts working from
where it was paused.
onResume()
◾ The Android onResume() method is invoked when the user starts
interacting with the user. This callback method is followed by
onPause(). Most of the functionalities of an application are
implemented using onResume().
onStop()

◾ The Android onStop() method is invoked when the activity is no longer


visible to the user. The reason for this state is either activity is getting
destroyed or another existing activity comes back to resume state.
onDestroy()

◾ The Android onDestroy() is the method that is called when an activity


finishes and the user stops using it. It is the final callback method
received by activity, as after this it is destroyed.
Write Log Messages
◾ The Log class allows you to create log
messages that appear in logcat. Generally,
you should use the following log methods,
listed in order from the highest to lowest
priority (or, least to most verbose):
TOASTS
A toast provides simple feedback
about an operation in a small
popup. It only fills the amount of
space required for the message
and the current activity remains
visible and interactive. Toasts
automatically disappear after a
timeout.

Toasts overview | Android Developers


Alternatives to using Toasts 2. NOTIFICATIONS
A notification is a message that Android displays
1. SNACKBAR outside your app's UI to provide the user with
Snackbars provide brief reminders, communication from other people, or
messages about app other timely information from your app. Users
processes at the bottom of can tap the notification to open your app or take
the screen. an action directly from the notification.
Real Time Situation

• When you open the app it will go through below states:


onCreate() –> onStart() –> onResume()
• When you press the back button and exit the app

onPaused() — > onStop() –> onDestory()


• When you press the home button
onPaused() –> onStop()
• After pressing the home button, again when you open the app from a recent task list
onRestart() –> onStart() –> onResume()
Real Time Situation …
• After dismissing the dialog or back button from the dialog
onResume()
• If a phone is ringing and user is using the app
onPause() –> onResume()
• After the call ends
onResume()
• When your phone screen is off
onPaused() –> onStop()
• When your phone screen is turned back on
onRestart() –> onStart() –> onResume()
Screen Orientation
(Horizontal / Vertical)
◾ Some device configurations change while the application is running,
such as when the device changes its orientation. Such a change
restarts the activity by calling all its methods again. So when the device
orientation changes, first the Activity will disappear for a millisecond
when the onPause(), OnStop, and onDestroy() methods are called.
◾ After a few milliseconds, the activity will be restarted when the
onCreate(), onStart() and onResume() methods are called.

◾ https://medium.com/hootsuite-engineering/handling-orientation-changes-on-android-41a6b62cb43f
References

◾ https://developer.android.com/guide/topics/manifest/manifest-intro

◾ https://www.studytonight.com/android/activity-in-android

◾ https://data-flair.training/blogs/android-activity/

Toasts overview | Android Developers

You might also like