There are two simple steps to creating a back button in the title bar:
First, make the application icon clickable using the following code in the exercise, in the title bar of which you want to add the "Back" button:
ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true);
After adding the above code, a back arrow will appear to the left of the application icon.

Secondly, after you have done the above, you still need to create code that will take advantage of the click of a button. To do this, onOptionsItemSelected in onOptionsItemSelected that when you really click on the application icon, the onOptionsItemSelected method is onOptionsItemSelected . Therefore, to return to the previous action, add this method to your action and insert Intent code in it, which will return you to the previous action. For example, suppose that you are trying to return MyActivity to MyActivity , called MyActivity . To get back to this, write a method as follows:
public boolean onOptionsItemSelected(MenuItem item){ Intent myIntent = new Intent(getApplicationContext(), MyActivity.class); startActivityForResult(myIntent, 0); return true; }
It!
(The API for Android developers recommends messing around with the manifest and adding things like android:parentActivityName . But this doesn't seem to work for me. The above is simpler and more reliable.)
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" />
And in your work
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Luke F. May 26 '13 at 12:58 a.m. 2013-05-26 00:58
source share