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
3
votes
1 answer
Refreshing fragment doesn't call onCreate(). It's normal?
I need to recreate a fragment when user press a toggle. I'm using this function:
public void refreshFragment(){
FragmentTransaction ft = getFragmentManager().beginTransaction();
if (Build.VERSION.SDK_INT >= 26) {
…

NullPointerException
- 36,107
- 79
- 222
- 382
3
votes
1 answer
Where to initiate views in Fragment? onViewCreated or onActivityCreated
So where is the best place to initiate views in a fragment? We know that we should only inflate a layout inside onCreate() and not initiate views, like setting listeners.
You should inflate your layout in onCreateView but shouldn't initialize other…

Reza Bigdeli
- 1,142
- 1
- 12
- 25
3
votes
1 answer
Distinguish first vs followings onCreateView's call in fragment
Probably it is a well-known topic, but I didn't find any "universal" solution.
The main problems are two:
When a fragment is put to backstack, onSavedInstanceState is never called
Why can't you instantiate fragment's views in onCreate?
And at this…

Pier Giorgio Misley
- 5,305
- 4
- 27
- 66
3
votes
2 answers
fragment lifecycle while navigating between fragments?
I do understand bit about fragment lifecycle, but I am finding me unable to visualize well the flow chart(or control diagram). When we move from one fragment to other. eg
TheFragment tf6 = new TheFragment(); // TheFragment class extends…

Saket
- 79
- 1
- 5
3
votes
0 answers
android - fragment backstack with bottomnavigationview onSaveInstanceState does not call properly
I have a problem with my fragment onSaveInstanceState. I searched on stackoverflow and other sites, I could not get exactly same problem with my issue.
I have a bottom navigation view ( which is not android support library bottom navigation view if…

okarakose
- 3,692
- 5
- 24
- 44
3
votes
1 answer
onCreateView is called after returning back from second activity
I have MainActivityFragment from where I start DetailActivity with below code:
private final CurrencyListAdapter.ClickListener itemClickListener
= new CurrencyListAdapter.ClickListener() {
@Override
public void onItemClick(int…

Orkhan Gasimli
- 103
- 1
- 11
3
votes
2 answers
Event on fragment replace?
I saw that when I execute
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.replace(R.id.parent, myFragment,…

helloimyourmind
- 994
- 4
- 14
- 30
3
votes
2 answers
Nested fragments created twice on orientation change
I created this example to understand the lifecycle of android fragments at screen orientation change.
MainActivity is a container for DrawerLayout, DrawerLayout allows you to choose a fragment, which will fill the MainActivity screen.
public class…

FilipR
- 1,218
- 4
- 22
- 39
3
votes
1 answer
Android Fragment onPause event called late with FragmentPagerAdapter
I have a FragmentPagerAdapter in an activity that manages 4 fragments, all is working fine, but the onPause, and onDestroyView events of each fragment, are not called immediatly after the new fragment is loaded ie :
FragmentA --> FragmentB -->…

Houssem
- 2,069
- 3
- 27
- 42
2
votes
1 answer
Android Fragment lifecycle orientation changes
I have an activity which should display Fragment only in portrait mode. So, my portrait layout have container-view and my landscape - don't. However, after changing orientation portrait->landscape Fragment is recreated even tho it have no container…

Paul Freez
- 1,059
- 15
- 27
2
votes
1 answer
When will onResume() be called without onStart() being called first (Fragment Lifecycle)
onStart() is when the Fragment becomes visible, onResume() is when the Fragment becomes interactive. So when will the onResume() -> onPause() -> onResume() cycle execute without executing the encompassing onStart() -> onStop() cycle?
In terms of…

xyals
- 347
- 1
- 2
- 9
2
votes
2 answers
How to keep a local service running in between tabs?
I am currently working on a Pedometer application. At first, I started with one activity, the PedometerActivity. This activity started the service that is supposed to run in the background, and binds to it. The code is long so I'll just give…
user7060819
2
votes
2 answers
Communication between two fragments with interface
My app have one activity A and two fragments F1 and F2.
One clicking button in F1, the F1 is replaced by F2.
I want to transfer data in F1 to F2. For that I implemented the interface in F1 and override its method in A.
But when sending the data from…

Gaurav Singh
- 125
- 3
- 12
2
votes
0 answers
LifeCycle of Fragment With ViewPager
07-02 01:24:00.530 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.MainActivity: onCreate
07-02 01:24:00.534 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentWithVP:…

q126y
- 1,589
- 4
- 18
- 50
2
votes
1 answer
Object reference not set to an instance of an object when trying to populate Xamarin Android TextView on fragmenrt from arrary
I'm getting the error System.NullReferenceException: Object reference not set to an instance of an object when I try to reference a TextView from my fragment. It happens at runtime.
Fragment
public class Fragment1 : Fragment
{
private int…

Vincent N.
- 99
- 1
- 12