Question

I want to put one back button in title bar area to go on back activity in Android app how to put that button.

enter image description here

Like there is in this image.

Was it helpful?

Solution

Assign a parent Activity in Manifest.xml like this :

<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>

Then in Activity's onCreate():

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

And at last in onOptionsItemSelected :

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

Here is more if you need :

Android Docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top