I want to go on home screen when I press device's back button.I am using this code..
public void onBackPressed() {
this.finish();
return;
}
Pressing the BACK key will effectively call finish()
for you. There is no need to trap the BACK key.
I'm assuming your problem is that when you press the BACK key it is simply going back to the previous Activity
in your app.
If that is the case then make all activities 'self-terminate' when they start a new Activity
in your app....
startActivity(new Intent(this, MyNewActivity.class));
finish();
If you do that then there will be no Activity
to return to when you press BACK and it will always return to the home screen.
You can try this
@Override
public void onBackPressed() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With