0% found this document useful (0 votes)
44 views8 pages

Activity and Fragment

Shhsshshsbah

Uploaded by

giratheayush
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)
44 views8 pages

Activity and Fragment

Shhsshshsbah

Uploaded by

giratheayush
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
You are on page 1/ 8

Activity and Fragment

Activity and Fragment

 An Activity is a user interface component that is mainly used to construct a single


screen of the application and represents the main focus of attention on a screen.
 An activity can host one or more fragments at a time.
 fragment is a kind of sub-activity.
 It is always hosted by an activity.
 It has its own layout and its own behavior with its own life cycle callbacks.
 We can add or remove fragments in an activity while the activity is running.
 Activity is the UI of an application through which user can interact and Fragment
is the part of the Activity, it is a sub-Activity inside activity which has its own
Life Cycle which runs parallel to the Activities Life Cycle.
Activity Lifecycle
Android activity lifecycle
onCreate()
You must implement this callback, which fires when the system first creates the activity.
On activity creation, the activity enters the Created state.
In the onCreate() method, perform basic application startup logic that happens only once
for the entire life of the activity.

onStart()
When the activity enters the Started state, the system invokes onStart().
This call makes the activity visible to the user as the app prepares for the activity to enter
the foreground and become interactive.
For example, this method is where the code that maintains the UI is initialized.

onResume()
When the activity enters the Resumed state, it comes to the foreground, and the system
invokes the onResume() callback. This is the state in which the app interacts with the
user. The app stays in this state until something happens to take focus away from the app,
such as the device receiving a phone call, the user navigating to another activity, or the
device screen turning off.
onPause()
The system calls this method as the first indication that the user is leaving your activity,
though it does not always mean the activity is being destroyed. It indicates that the activity
is no longer in the foreground

onStop()
When your activity is no longer visible to the user, it enters the Stopped state, and the
system invokes the onStop() callback. This can occur when a newly launched activity
covers the entire screen. The system also calls onStop() when the activity finishes running
and is about to be terminated.

onDestroy()
onDestroy() is called before the activity is destroyed. The system invokes this callback for
one of two reasons:

The activity is finishing, due to the user completely dismissing the activity or due to
finish() being called on the activity.
The system is temporarily destroying the activity due to a configuration change, such as
device rotation or entering multi-window mode.
Fragment Lifecycle
Each Fragment instance has its own lifecycle. When a user navigates and interacts with your
app, your fragments transition through various states in their lifecycle as they are added,
removed, and enter or exit the screen.

The FragmentManager class is responsible to make interaction between fragment objects.


No. Method Description
1) onAttach(Activity) it is called only once when it is attached with activity.
2) onCreate(Bundle) It is used to initialize the fragment.
3) onCreateView(LayoutInflater, creates and returns view hierarchy.
ViewGroup, Bundle)
4) onActivityCreated(Bundle) It is invoked after the completion of onCreate( ) method.
5) onStart( ) makes the fragment visible.
6) onResume( ) makes the fragment interactive.
7) onPause( ) is called when fragment is no longer interactive.
8) onStop( ) is called when fragment is no longer visible.
9) onDestroyView( ) allows the fragment to clean up resources.
10) onDestroy( ) allows the fragment to do final clean up of fragment state.
11) onDetach( ) It is called immediately prior to the fragment no longer being
associated with its activity.
Activity Fragment
Activity is an application component that gives The fragment is only part of an activity, it
a user interface where the user can interact. basically contributes its UI to that activity.
Fragment is dependent on activity. It can’t exist
Activity is not dependent on fragment
independently.
we need to mention all activity it in the Fragment is not required to mention in the
manifest.xml file manifest file
We can’t create multi-screen UI without using After using multiple fragments in a single
fragment in an activity, activity, we can create a multi-screen UI.
Activity can exist without a Fragment Fragment cannot be used without an Activity.
While Using fragments in the project, the
Creating a project using only Activity then it’s
project structure will be good and we can
difficult to manage
handle it easily.
Lifecycle methods are hosted by OS. The Lifecycle methods in fragments are hosted by
activity has its own life cycle. hosting the activity.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy