0

I want to add the back key in my application so I added this

onCreate(){
        getActionBar().setDisplayHomeAsUpEnabled(true);
}

And this

        if(id == android.R.id.home){
            this.finish();
            Intent intent = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(intent);
        }

It works fine when i press the back button from the App But when I press physical back button key it kill the application

Kirtesh
  • 13
  • 7

2 Answers2

0

There is a difference between the back button and up button. In your post you've mentioned about up button. See Up vs Back in android documentation. For handling the back key, You might need to override onBackPressed() in your activity to handle the situation. See How to handle back button in activity.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

Override this,

@Override
public void onBackPressed(){
    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
    startActivity(intent);
    finish();
}
Apurva
  • 7,871
  • 7
  • 40
  • 59