In the Android v7 package, the Toolbar and ActionBarActivity implement the backward navigation effect. actionbaractivityv7

Source: Internet
Author: User

In the Android v7 package, the Toolbar and ActionBarActivity implement the backward navigation effect. actionbaractivityv7

The ToolBar and ActionBarActivity In the android. support. v7 package both come with the back navigation button. You only need to manually enable it for display.
Let's take a look at the ToolBar, the front-end code of the page:
<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" />

To display the back button, add the following code in the background:

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Of course, after this line of code is added, there is only a back arrow. After clicking it, it does not respond. You need to add a listening event. The default button id is fixed. android. r. id. home, add the following listening event in onOptionsItemSelected.

     case android.R.id.home:              finish();              break;

In fact, this does not actually return the previous page, but destroys the page to display the previous page, that is, jump to the previous page.

How can I set ActionBarActivity? See the following:

First, set this page as the page to be returned in page.

    setHomeButtonEnabled

In the configuration file, set parentActivityName of page B to page A, but this attribute is only available after API 16. meta-data is used before.

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

To display the back button in page B, add code

        ActionBar actionBar=getSupportActionBar();        actionBar.setDisplayHomeAsUpEnabled(true);    

In this way, A backward arrow is displayed in the upper left corner of the title bar of page B. After you click it, page A is automatically returned, and no additional listening events need to be added.

Conclusion: It seems that ActionBarActivity's built-in back navigation is more convenient than ToolBar, but so far, google does not recommend using ActionBarActivity. Now, creating an Activity in AS inherits AppCompatActivity, I don't know why I am a beginner. I also try to implement the auto-backend effect of the ToolBar, but although the ToolBar also has the corresponding setHomeButtonEnabled method and setDisplayHomeAsUpEnabled method, it does not work, what google finds is to add listening events, and there is no saying that it can implement ActionBarActivity-like effects, and finally give up.

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.