0

I have an activity with action bar and I want return to the previuos one, how can I do it using action bar?

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
user3084416
  • 78
  • 1
  • 6
  • you need enable home button on action bar and on click of that call finish method, see this for enable home button http://stackoverflow.com/questions/16150205/android-action-bar-home-button. see this too for more info http://stackoverflow.com/questions/10108774/how-to-implement-the-android-actionbar-back-button – Shayan Pourvatan May 17 '14 at 13:34

1 Answers1

5

Add getActionBar().setDisplayHomeAsUpEnabled(true);.

and override onOptionsItemSelected() like below :

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Also specify parent activity in manifest file :

For example:

<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" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>
vjdhama
  • 4,878
  • 5
  • 33
  • 47