Android V7 Package toolbar and actionbaractivity for back navigation effects

Source: Internet
Author: User

The toolbar and actionbaractivity under the ANDROID.SUPPORT.V7 package come with a back navigation button, just open it manually and let it show.
First look at the toolbar, page front code:
< Android.support.v7.widget.Toolbar             Android:id = "@+id/toolbar"             android:layout_width= "Match_parent"            android:layout_height= "? attr/ Actionbarsize "            android:background="? attr/colorprimary "            app: Popuptheme= "@style/apptheme.popupoverlay"/>

If you want the back button to appear, you need to add the following code in the background

        Getsupportactionbar (). setdisplayhomeasupenabled (true);

Of course, after adding this line of code is just a backward arrow only, after the click did not respond, need to continue to add listening events before the line, the default added button ID is fixed, Android. R.id.home, add the following listener events in the onoptionsitemselected

 Case Android. R.id.home:              finish ();               break;

In fact, this is not the real return to the previous page, but the page is destroyed, thus displaying the previous page, that is, the page before the jump.

So actionbaractivity how to set it up, look at the following:

First, set the page to be returned in page A that you want to return.

Sethomebuttonenabled

The parentactivityname of page b in the manifest file is then set to page A, but this property is only available after API 16, which is used before the meta-data.

<meta-data android:name= "Android.support.PARENT_ACTIVITY"                android:value= ". Mainactivity "></meta-data>

To display the Back button in page b, add the code

        ActionBar actionbar=Getsupportactionbar ();        Actionbar.setdisplayhomeasupenabled (true);    

This displays a back arrow in the upper-left corner of the title bar of page B, which automatically returns to page a when clicked, and does not need to add additional listener events.

Summary: It seems that Actionbaractivity's back navigation is more convenient than toolbar, but so far, Google has not recommended the use of actionbaractivity, Now the new activity in as is inherited by Appcompatactivity, and as for why I don't know this beginner, I also try to realize toolbar's auto-rewind effect, But although toolbar also has the corresponding sethomebuttonenabled method and setdisplayhomeasupenabled, but it does not work, Google found after all said to add listening events, Did not find to say can achieve similar actionbaractivity effect, finally give up.

Android V7 Package toolbar and actionbaractivity for back navigation effects

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.