0

I've an Activity with just these methods:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_week);

    getActionBar().setDisplayHomeAsUpEnabled(true);
}

that shows carette in the action bar, and

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

'couse I want to finish(); this activity after pressing carette simbol. But nothing happens when i press back button with my finger. What's wrong?

1
  • leave just the line: finish(); onBackPressed() Commented Nov 22, 2013 at 10:29

3 Answers 3

5

If you want to finish the activity when option bar's back button clicked you need to add following code.

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

Hardware back will work by default. You can remove

onBackPressed()

method or edit as like this

@Override
public void onBackPressed() {
        finish();
}
2

Use this:

@Override
public void onBackPressed() {
        finish();
}
0

the statement: this.finish() is sufficient to do this..

You can delete this one: super.onBackPressed();

Not the answer you're looking for? Browse other questions tagged or ask your own question.