Subtitle of the android-Action Bar

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.