Covers how and when the lifecycle callback methods like onAttach, onCreateView etc. are being called when the fragment is created, destroyed, recreated or put in the backStack.
Questions tagged [fragment-lifecycle]
150 questions
373
votes
3 answers
Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments
What are the differences between onCreate(), onCreateView(), and onActivityCreated() in fragments and what would they each be used for?

Farbod Salamat-Zadeh
- 19,687
- 20
- 75
- 125
47
votes
5 answers
Why getContext() in fragment sometimes returns null?
Why getContext() sometimes returns null? I pass context to LastNewsRVAdapter.java as an argument. But LayoutInflater.from(context) sometimes crashes. I'm getting a few crash reports on play console. Below is crash…

Bek
- 7,790
- 4
- 18
- 31
38
votes
4 answers
Navigation Component prevent to recreate fragment on back press
I'm using the Jetpack Navigation Component in my project with a single activity and some fragments.
I have a fragment with a list that fills from server side. I call getDataFromServer on the onViewCreated method then, when a user clicks on an item,…

FarshidABZ
- 3,860
- 4
- 32
- 63
27
votes
3 answers
Understanding Fragment's lifeCycle methods calls during fragment transaction
I created a demo to understand which all fragment lifecycle's methods are called during different cases of fragment transaction.While most of the calls are as per expectation few things I am still confused which I have written in Bold.
Suppose two…

Android Developer
- 9,157
- 18
- 82
- 139
26
votes
3 answers
Restoring MapView's state on rotate and on back
Background
I have a larger application in which I had/have several problems with new Google Maps API. I tried to describe it in a different question but since it seems too complex I decided to start a new project, as simple as possible and try to…

Izydorr
- 1,926
- 3
- 23
- 40
21
votes
4 answers
Fragment lifecycle events in LifeCycleAware Fragment
I have a lifecycle aware fragment and a LifecycleObserver class
public class MyFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
…
user4260260
21
votes
2 answers
Difference between add() & replace() with Fragment's lifecycle
My program has 6 fragments: Fragment1, Fragment2,....->Fragment6.
I use the add() and replace() statement to switch between the fragment and track their lifecycle.
Fragment1 add Fragment2 add Fragment3 add Fragment4 add Fragment5 replace…

thanhnh
- 241
- 1
- 2
- 5
18
votes
4 answers
Stop handler after the fragment has been destroyed
I have a Fragment which sets up a ListView and creates a Handler to update the Listview periodically. However, it looks like the Handler still runs after the Fragment has been destroyed.
The following is the code.
@Override
public View…

Sandah Aung
- 6,156
- 15
- 56
- 98
17
votes
4 answers
Fragment lifecycle overlap on navigate
I have a single Activity application with multiple Fragments that are being switched by using Navigation components. When I switch between two fragments their onCreate() and onDestroy() methods seem to overlap. Thus making it difficult for me to…

rozina
- 4,120
- 27
- 49
14
votes
1 answer
IllegalArgumentException with Otto Event bus in Fragment instance
I am using Otto Event bus to subscribe to certain events in a ListFragment. The bus instance is stored and created in an subclass of Application, in other words, it bus should work as a singleton. It seems like this is not a case...
The fragment is…

bjorncs
- 1,250
- 11
- 20
9
votes
2 answers
injecting viewmodel with navigation-graph scope: NavController is not available before onCreate()
I'm using a navigation-component in my application and also using shared ViewModel between multiple fragments that are in the same graph. Now I want to instantiate the ViewModel with this graph scope with this.
As you know, in fragments we should…

beigirad
- 4,986
- 2
- 29
- 52
8
votes
2 answers
How to get LifecycleOwner in a Fragment?
I need to get the LifecycleOwner to pass it in my FirestoreRecyclerViewOption but Fragment doesn't implement Lifecycle.
So, how to get that?
My Fragment implements LifecycleOnwer as shown in the following code example,
public class ListRestFragment…

Alek KT
- 134
- 1
- 1
- 9
7
votes
4 answers
onDestroyView For Fragment Never Called After onStop
My APP does have multiple fragments and activities, Most of these activities hold different fragment, This is to make my components reusable easily. I am facing an issues when I'm loading other activities to the activity stack.
Case
Launched…

CodeDecode
- 401
- 2
- 10
- 19
7
votes
1 answer
Why does oncreateview of the current fragment gets called on pressing the back button?
I am trying to retrieve the fragment from the backstack. It is getting retrieved but the issue is that on pressing the back button the oncreate view and subsequent lifecyce methods of current fragment is also getting called. Here's my code to put…

volunteer softwares
- 81
- 2
- 5
7
votes
2 answers
Save interface (Listener) in onSaveInstanceState
SaveInstanceState
For data like Integer, Long, String and else are fine, I just put it in the bundle and get it back once the onCreateView gets called again. But my fragment also has listener like following,
public class SomeFragment extends…

Tar_Tw45
- 3,122
- 6
- 35
- 58