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.
Quoting from Official Guideline
FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
FLAG_ACTIVITY_CLEAR_TASK
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
FLAG_ACTIVITY_NO_HISTORY
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.
FLAG_ACTIVITY_NEW_TASK
If set, this activity will become the start of a new task on this history stack.
@Override
public void onBackPressed()
{
Intent intCloseApp = new Intent(Intent.ACTION_MAIN);
intCloseApp.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intCloseApp.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intCloseApp.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intCloseApp.addCategory(Intent.CATEGORY_HOME);
intCloseApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intCloseApp);
}
Easy way is to just finish the current activity when you are moving to the next activity.
Just do a finish()
after your startActivity()
, then on back press from the any activity you can simply call finish()
to come out of the application as all the other activities are not there in the stack.
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);
}
Ok, for launching Home sceen of your device use this code in your onKeyDown()
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
And if you want to close your application then I think either you have to close your all activities by finish()
in a manner (some standard way) or using
android.os.Process.killProcess(android.os.Process.myPid())
this code kill your app.
(Some ugly way..!)
public void onBackPressed() {
System.exit(1);
return;
}
the Brutal way