You are on page 1of 16

Lifecycle in Android Architecture

Components
Unit 4
• Lifecycle is one of the 
Android Architecture Components which was
released by Google to make it easier for all the
Android developers. The Lifecycle is a
class/interface which holds the information
about the state of an activity/fragment and
also it allows other objects to observe this
state by keeping track of it.
• The LifeCycle component is concerned with
the Android LifeCycle events of a component
such as an Activity or a Fragment, it has three
main classes that we’ll deal with:
• Lifecycle
• Lifecycle Owner
• Lifecycle Observer
Lifecycle
• Lifecycle is a process that tells us about
the Events performed on an
Activity/Fragment.
• We have a lifecycle as a class that has two
types of enumerations to track the
components, State and Event.
• Event and State are used to determine the
lifecycle. Each event has its own state.
•  OnCreate()Called when the activity is first created. 
• OnStart()Called when the activity becomes visible to the user. 
• OnResume()   Called when the activity starts interacting with the
user. 
• OnPause()Called when the current activity is being paused and
the previous activity is resumed. 
• OnStop()Called when the activity is no longer visible to the user. 
• OnDestroy()Called before the activity is destroyed by the
system(either manually or by the system to conserve memory 
• OnRestart()Called when the activity has been stopped and is
restarting again.
Android Life Cycle
Lifecycle Owner
• Every Activity has a Lifecycle. This Activity will be a Lifecycle
Owner(any activity or fragment can be called a lifecycle owner).
• When an activity is initialized LifecycleOwner is the one that will
be constructed initially. Any class that implements
the LifeCycleOwner interface indicates that it has Android
LifeCycle.
• For example, starting from Support Library 26.1.0 Fragments
 and Activities implement the LifeCycleOwner interface.
• One can create custom LifeCycleOwner components by
implementing the interface and using a LifeCycleRegistry as
described here.
Lifecycle Observer
• Lifecycle Observer, which observes the activity
and keeps track of the lifecycle, and performs an
action.
• The action performed by this lifecycle Observer
depends on the lifecycle of the lifecycle Owner.
• Every lifecycle owner has a lifecycle and based on
the event or state of the lifecycle of the owner,
the lifecycle observer performs the action.
Android Activity Lifecycle
• Android Activity Lifecycle is controlled by 7 methods
of android.app.Activity class. The android Activity is
the subclass of ContextThemeWrapper class.
• An activity is the single screen in android. It is like
window or frame of Java.
• By the help of activity, you can place all your UI
components or widgets in a single screen.
• The 7 lifecycle method of Activity describes how
activity will behave at different states.
Android TabLayout
• TabLayout is used to implement horizontal tabs. TabLayout
is released by Android after the deprecation
of ActionBar.TabListener (API level 21).
• TabLayout is introduced in design support library to
implement tabs.
• Tabs are created using newTab() method of TabLayout
class. The title and icon of Tabs are set
through setText(int) and setIcon(int) methods of
TabListener interface respectively. Tabs of layout are
attached over TabLayout using the method addTab(Tab)
method.
Example of Tab Layout
Basic Views
• In Android applications, various types of
ViewGroups are used to design UI.
• Basic Views
• Picker Views
• List Views
• Specialized Fragments
• Analog and Digital Clock Views
Basic Views
• In Android applications, the following are Basic Views.
• TextView
• EditText
• Button
• ImageButton
• CheckBox
• ToggleButton
• RadioButton
• RadioGroup
• Implementation
Android  Basic views section
• Android  Basic views section would include following
items:
• Android EditText
• Android TextBox
• Android Button
• Android Radio Button
• Android Checkbox
• Android Toggle Button
• Android Radio Group
• Android Image Button
Basic views section
• Button: Button is pushed or pressed or clicked. We already know how
to use button widget but let us know what it is. We have attributes
related to buttons. Every view should have a unique identifier which
identifies the element in the project. Make sure, two elements should
not share their unique ID. We implement certain methods and
interfaces which listens to user input and act accordingly.
• TextView: This view is basically meant for displaying the text of
application. For example the label of application is displayed here.
• EditText: This is a modification of Text View. It is editable. User can
give input dynamically.
• Check Box: This widget is a two-state button. It can be either checked
or unchecked.
Basic views section
• Radio Button: This is also a two-state button. It can be
checked or unchecked. Unlike checkbox if it is checked
once, cannot be unchecked. It can be checked
dynamically.
• Radio Group: This houses radio buttons together.
Checking any one of the radio buttons in the group
unchecks rest of the buttons. 
• Image Button: This a button with image. User can
press or click this button. Image on the button is
referenced from the src folder of our project.

You might also like