28

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?

Vikram Bodicherla
  • 7,133
  • 4
  • 28
  • 34

5 Answers5

16

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

TapanHP
  • 5,969
  • 6
  • 37
  • 66
16

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

Shog9
  • 156,901
  • 35
  • 231
  • 235
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
  • Thanks, I don't know how I missed that one out. – Vikram Bodicherla Sep 27 '11 at 06:11
  • I have Activity1--->Fragment1--->(Nested)fragment2<===>Activity2 when I'm on Activity2 when I press the back button fragment2(Activity1) at that time I need to know which method is fired in fragment2? I just want to refresh fragment2 content when user click back button on Activity2! – LOG_TAG Nov 27 '13 at 04:32
  • @LOG_TAG: "onResume" will be fired. – Mehul Joisar Dec 02 '13 at 14:42
0

when you are at Activity2---->backpress--->Fragment2(Activity1)---means Activity1 again attach from fragment2 so on OnAactivityCreated() method Activity1 is completely loaded ....so at that we can call setData() method of your Activity1...

Rajesh
  • 10,318
  • 16
  • 44
  • 64
0

onAttachFragment()-activity is called before onCreate()-activity and after onAttach()-fragment

David Aleksanyan
  • 2,953
  • 4
  • 29
  • 39
-1

Call onDestroy on onStop of your fragment. This should call onCreate when the fragment is launched. Let me know if works as an ideal solution for your problem.

BTR Naidu
  • 1,063
  • 3
  • 18
  • 45