Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreateOptionsMenu in a Fragment lifecycle

I found a really strange behavior in the Fragment lifecycle.

After several tests on one Fragment, I realized than I got this execution order every time:

  1. onCreateView()
  2. onStart()
  3. onCreateOptionsMenu()

I am really surprised because I was convinced that onStart would only be called after the execution of onCreateOptionsMenu.

FYI, I was trying to set a MenuItem as a global class variable in onCreateOptionsMenu to be able to use it in onStart. Of course I got a null pointer because of the execution order explained above.

It seems the more I dig into the Fragment's lifecycle, the more I am lost and the documentation only details the main methods, never the ones such as onCreateOptionsMenu, onPrepareOptionsMenu, onCreateContextMenu etc.

Does someone has any additional information about this, the documentation is not really helpful...

Thank you

like image 760
Yoann Hercouet Avatar asked Apr 26 '14 11:04

Yoann Hercouet


1 Answers

Check this out https://github.com/xxv/android-lifecycle. There you can see a full lifecycle for both fragment and activity.

like image 151
Javier Mendonça Avatar answered Oct 13 '22 08:10

Javier Mendonça