Our goal is to add an option menu to the right of the Action bar and click it to show or hide the subtitle of the Action Bar.
Because the action Bar appears after the API11 level, compatibility issues must be considered. We directly let the device below API11 not see the Options menu at all. Set up res/menu-v11, put the menu XML into it,
Add the item on the Options menu.
Then the old step: 1.setHasOptionsMenu (True)
2.onCreateOptionsMenu ()
3.onOptionsItemSelected ()
Seemingly perfect solves the problem, there is actually a serious bug. That's the classic device rotation problem. If the subtitle is already displayed, the subtitle will be empty after rotation, and the title of the option menu will change Show_subtitle
How to solve it? Some people think of saving fragment instances so that they remain in the state during device rotation. It's a great idea, but it's a little bit neglected, whether the subtitle or the display of the Options menu is a problem with the view, regardless of whether the fragment instance will be saved, thefragment view will be destroyed because of the resource reconfiguration possibilities.
No matter if there is a subtitle before, the view will not be rebuilt. So we need to add a flag in fragment to store the subtitle display. Determines whether a subtitle needs to be displayed when the view is rebuilt based on the flag.
Finally, to summarize, the view layer displays the data if it is set in a non-create view method, then after the device rotation changes the configuration behavior, because of the destruction of the view of the reconstruction, even if the fragment instance has been preserved, also in vain.
Subtitle of the android-Action Bar