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;
}
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);
}
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..!)
You should override finish()
@Override
public void finish() {
System.out.println("finish activity");
SaveData();
System.runFinalizersOnExit(true) ;
super.finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
Then invoke this method like this:
@Override
public void onBackPressed() {
this.finish();
}
When you call NewAcitivy call finish()
.After startActivity
so the previous acitivity gets closed.
Then use
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
You can try this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK){
this.finish ();
}
return true;
}
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 close application from anywhere to call this type of Intent:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This is the best way for close application from anywhere, on any click event
You need to start an intent using a flag to clear the activities on top of the actual activity.
public void onBackPressed() {
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
this.finish();
}
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);
}
u want to Override the OnkeyDown & finish activity
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}