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