6

I tried this code to show my ProfileActivity:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_profile) {
            Intent mainIntent = new Intent(Contracts.this, Profile.class);
            startActivity(mainIntent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

When I click on Profile menu Item I can see my ProfileActivity but I not have any back button on my ActionBar to return in my previous activity.

So, how can I show some back button? Is correct my code to display an activity from my menu?

Sufian
  • 6,405
  • 16
  • 66
  • 120
Safari
  • 11,437
  • 24
  • 91
  • 191
  • possible duplicate of [display back button on action bar - android](http://stackoverflow.com/questions/15686555/display-back-button-on-action-bar-android) – Sufian Feb 26 '15 at 11:50

2 Answers2

9

At your Profile Activity check if there's something missing:

public class Profile extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // etc...
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.action_profile:
            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>

http://developer.android.com/design/patterns/navigation.html#up-vs-back

Machado
  • 14,105
  • 13
  • 56
  • 97
  • Not work: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mybusiness.myapp/com.mybusiness.myapp.Profile}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference – Safari Feb 26 '15 at 12:01
  • 3
    The problem is pretty simple- your activity is inheriting from the new `android.support.v7.app.ActionBarActivity`. We should be using a call to `getSupportActionBar()` instead of `getActionBar()`. – Machado Feb 26 '15 at 12:44
5

In onCreate() method of Profile.class put this,

getActionBar().setDisplayHomeAsUpEnabled(true);

and this,

@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{       
    onBackPressed();
    return true;
}
Apurva
  • 7,871
  • 7
  • 40
  • 59
  • 1
    Not work: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mybusiness.myapp/com.mybusiness.myapp.Profile}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference – Safari Feb 26 '15 at 12:03
  • For completeness I would add the `finish()` call in the `onOptionsItemSelected` to illustrate how you can go back to the previous activity. – KBog Oct 06 '20 at 01:15