Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment lifecycle with respect to it's activity

Situation My activity waits on an Async operation and after it hears back from async operation, it needs to pass information to 2 fragments inside it.

Requirement 1. Both fragments need their onCreateView calls to be done for them to have their layouts loaded, 2. They need for themselves to be attached to their activity so that getActivity() works.

I wrote a setData() method in both the fragments and am looking for the "correct" place in the activity's lifecycle to invoke them.

onCreate() of the activity does not work, onStart() of the activity does not work and onStart() of the fragment does not work.

Nothing works, what am I missing here?

like image 216
Vikram Bodicherla Avatar asked Sep 21 '11 08:09

Vikram Bodicherla


People also ask

How Fragment life cycle is related with activity life cycle?

A fragment life cycle is closely related to the lifecycle of its host activity which means when the activity is in the pause state, all the fragments available in the activity will also stop. Fragments added to the Android API in Android 3.0 which API version 11 to support flexible UI on large screens.

What is fragment fragment lifecycle activity in detail?

Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one fragment in an activity. Fragments represent multiple screen inside one activity. Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity.


2 Answers

The official documentation for the Fragment lifecycle explains this clearly - please refer to it and then ask follow-up questions if something is unclear.

like image 127
Vineet Shukla Avatar answered Sep 21 '22 20:09

Vineet Shukla


This Image will be helpful to understand both life cycles together.

As many people complaints and it is somewhat valid argument that this life cycle is too complicated, in Google I/O 2018,They have suggested to use Architecture component Framework. Please check this Docs

enter image description here

like image 25
TapanHP Avatar answered Sep 22 '22 20:09

TapanHP