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.