How to implement the Android ActionBar back button? How to implement the Android ActionBar back button? android android

How to implement the Android ActionBar back button?


Selvin already posted the right answer. Here, the solution in pretty code:

public class ServicesViewActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // etc...        getActionBar().setDisplayHomeAsUpEnabled(true);    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {        case android.R.id.home:            NavUtils.navigateUpFromSameTask(this);            return true;        default:            return super.onOptionsItemSelected(item);        }    }}

The function NavUtils.navigateUpFromSameTask(this) requires you to define the parent activity in the AndroidManifest.xml file

<activity android:name="com.example.ServicesViewActivity" >    <meta-data     android:name="android.support.PARENT_ACTIVITY"     android:value="com.example.ParentActivity" /></activity>

See here for further reading.


Make sure your the ActionBar Home Button is enabled in the Activity:

Android, API 5+:

@Overridepublic void onBackPressed() {     ...     super.onBackPressed();}

ActionBarSherlock and App-Compat, API 7+:

@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {    ...    getSupportActionBar().setDisplayHomeAsUpEnabled(true);}

Android, API 11+:

@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {    ...    getActionBar().setDisplayHomeAsUpEnabled(true);}

Example MainActivity that extends ActionBarActivity:

public class MainActivity extends ActionBarActivity {    @Override    public void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // Back button        getSupportActionBar().setDisplayHomeAsUpEnabled(true);    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {        case android.R.id.home:             // API 5+ solution            onBackPressed();            return true;        default:            return super.onOptionsItemSelected(item);        }    }}

This way all the activities you want can have the backpress.

Android, API 16+:

http://developer.android.com/training/implementing-navigation/ancestral.html

AndroidManifest.xml:

<application ... >    ...    <!-- The main/home activity (it has no parent activity) -->    <activity        android:name="com.example.myfirstapp.MainActivity" ...>        ...    </activity>    <!-- A child of the main activity -->    <activity        android:name="com.example.myfirstapp.DisplayMessageActivity"        android:label="@string/title_activity_display_message"        android:parentActivityName="com.example.myfirstapp.MainActivity" >        <!-- The meta-data element is needed for versions lower than 4.1 -->        <meta-data            android:name="android.support.PARENT_ACTIVITY"            android:value="com.example.myfirstapp.MainActivity" />    </activity></application>

Example MainActivity that extends ActionBarActivity:

public class MainActivity extends ActionBarActivity {    @Override    public void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // Back button        getSupportActionBar().setDisplayHomeAsUpEnabled(true);    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {        // Respond to the action bar's Up/Home button        case android.R.id.home:            NavUtils.navigateUpFromSameTask(this);            return true;        }        return super.onOptionsItemSelected(item);    }}


To enable the ActionBar back button you obviously need an ActionBar in your Activity.This is set by the theme you are using. You can set the theme for your Activity in the AndroidManfiest.xml. If you are using e.g the @android:style/Theme.NoTitleBar theme, you don't have an ActionBar. In this case the call to getActionBar() will return null. So make sure you have an ActionBar first.

The next step is to set the android:parentActivityName to the activity you want to navigate if you press the back button. This should be done in the AndroidManifest.xml too.

Now you can enable the back button in the onCreate method of your "child" activity.

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    getActionBar().setDisplayHomeAsUpEnabled(true);}

Now you should implement the logic for the back button. You simply override the onOptionsItemSelected method in your "child" activity and check for the id of the back button which is android.R.id.home.

Now you can fire the method NavUtils.navigateUpFromSameTask(this); BUT if you don't have specified the android:parentActivityName in you AndroidManifest.xml this will crash your app.

Sometimes this is what you want because it is reminding you that you forgot "something". So if you want to prevent this, you can check if your activity has a parent using the getParentActivityIntent() method. If this returns null, you don't have specified the parent.

In this case you can fire the onBackPressed() method that does basically the same as if the user would press the back button on the device. A good implementation that never crashes your app would be:

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    switch (item.getItemId()) {        case android.R.id.home:            if (getParentActivityIntent() == null) {                Log.i(TAG, "You have forgotten to specify the parentActivityName in the AndroidManifest!");                onBackPressed();            } else {                NavUtils.navigateUpFromSameTask(this);            }            return true;        default:            return super.onOptionsItemSelected(item);    }}

Please notice that the animation that the user sees is different between NavUtils.navigateUpFromSameTask(this); and onBackPressed().

It is up to you which road you take, but I found the solution helpful, especially if you use a base class for all of your activities.