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.

Était-ce utile?

La 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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top