Android ActionBar Home Button Two ways to return event handling

Source: Internet
Author: User

Bored this morning to look at the official Android documents, recently interested in Actionbar, it is really a very convenient for our daily development.

For the home button to go back by clicking Actionbar, I've only known one way:

Through Getsupportactionbar().  Setdisplayhomeasupenabled(true); Set Actionbar's Home button to click,    
@OverridePublic Booleanonoptionsitemselected(MenuItemItem) {Handle presses on the action bar itemsSwitch (Item.Getitemid()) {Case R.. Action_home: Android. R.id.home           //Processing return logic       & nbsp     return true;default:            return super.item}}        

The second is also just on the official website to see, in this record, to prevent later forget: In the manifest clearly marked the child activity of the parent relationship activity

<application...>...<!--the main/home activity (it has no parent activity)--<activityAndroid:name="Com.example.myfirstapp.MainActivity"...>...</activity><!--A Child of the main activity--<activityAndroid:name="Com.example.myfirstapp.DisplayMessageActivity"Android:label="@string/title_activity_display_message"Android:parentactivityname= "com.example.myfirstapp.MainActivity"   >        <!--Parent activity meta-data to support 4.0 and L Ower-->        <meta-data            android:name=             Android:value= "com.example.myfirstapp.MainActivity"  />    </activity></APPLICATION>            

Then call the Home button in the Oncreat () method to click:

@OverridePublic voidOnCreate(BundleSavedinstancestate) {    super.savedinstancestate (r. Layout. Activity_displaymessage    getsupportactionbar ().  Setdisplayhomeasupenabledtrue//If your minsdkversion is one or higher, instead use:    
                               
                                //Getactionbar (). Setdisplayhomeasupenabled (True); 
                                }   
                                         /span>              

Then we don't have to do any other processing, and when we click on the Home button, the Android system will automatically return the parent activity for us.

Android ActionBar Home Button Two ways to return event handling

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.