By Alvin Alexander. Last updated: June 4, 2016
As a quick “note to self” about the Android Fragment lifecycle, including when the onCreateOptionsMenu
method is executed, I have this debug output from an "Images GridView" in my current application.
The first time I go to the Images GridView:
04-13 14:59:29.484 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onCreate 04-13 14:59:29.485 2560-2560/com.alvinalexander.myapp I/VPFetchItemsTask﹕ ENTERED FetchItemsTask constructor 04-13 14:59:29.485 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onCreateView 04-13 14:59:29.492 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onResume 04-13 14:59:29.492 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onCreateOptionsMenu 04-13 14:59:29.610 2560-2560/com.alvinalexander.myapp I/VPFetchItemsTask﹕ LEAVING FetchItemsTask::onPostExecute 04-13 14:59:29.935 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onCreateOptionsMenu
When I close the app with the middle Android button:
04-13 15:00:34.330 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onPause 04-13 15:00:34.348 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onStop
When I come back to the app, with the Images GridView showing:
04-13 15:01:29.013 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onResume
When I left Images and went to Quotes:
04-13 15:02:08.077 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onCreateOptionsMenu 04-13 15:02:10.913 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onPause 04-13 15:02:10.913 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onStop 04-13 15:02:11.357 2560-2572/com.alvinalexander.myapp W/art﹕ Suspending all threads took: 5.694ms
When I come back to Images:
04-13 15:02:50.652 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onCreate 04-13 15:02:50.652 2560-2560/com.alvinalexander.myapp I/VPFetchItemsTask﹕ ENTERED FetchItemsTask constructor 04-13 15:02:50.652 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onCreateView 04-13 15:02:50.654 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onResume 04-13 15:02:50.655 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onCreateOptionsMenu 04-13 15:02:50.711 2560-2560/com.alvinalexander.myapp I/VPFetchItemsTask﹕ LEAVING FetchItemsTask::onPostExecute 04-13 15:02:51.087 2560-2560/com.alvinalexander.myapp I/VPImagesGridFragment﹕ ENTERED onCreateOptionsMenu
As you can see from those examples, the onCreateOptionsMenu
method is called after the onCreate
, onCreateView
, and onResume
lifecycle methods.