Suppose I have A->B->C->D activities.
I want to exit from C always onBackPressed()
.
How to exit from application on such situation. I have tried FLAG_ACTIVITY_CLEAR_TOP
but no success.
My Minimum SDK is API 8
.
Suppose I have A->B->C->D activities.
I want to exit from C always onBackPressed()
.
How to exit from application on such situation. I have tried FLAG_ACTIVITY_CLEAR_TOP
but no success.
My Minimum SDK is API 8
.
Use this code.
@Override
public void onBackPressed()
{
// TODO Auto-generated method stub
super.onBackPressed();
finish();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startMain);
}
public void AppExit()
{
this.finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
/*int pid = android.os.Process.myPid();=====> use this if you want to kill your activity. But its not a good one to do.
android.os.Process.killProcess(pid);*/
}
call this method when you click back
button
If you want to exit only onBackPressed() in C then all you have to do is override the onBackPressed() in activities A, B and D by doing this
@Override
public void onBackPressed()
{
// TODO Auto-generated method stub
}
This will ensure that when back key is pressed in A, B and D, application doesn't close. You don't need to override onBackPressed() in activity C as Android calls finish() by default. But if you want to pop up an alert dialog then you can do as follows -
@Override
public void onBackPressed()
{
// TODO Auto-generated method stub
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setTitle("Are you sure you want to exit the application");
alertbox.setCancelable(false);
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
finish();
}
});
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertbox.show();
}
Finally to take care that previous activities close when you try to finish the activity in C, you should call finish() whenever you start a new activity.
Try this:- In Activity A,
startActivity(new Intent("B"),request_code);
In onActivityResult() function,
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_code)
{
if(resultCode == RESULT_OK)
{
finish();
}
}
}
Now in Activity B,
startActivity(new Intent("C"),request_code);
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_code)
{
if(resultCode == RESULT_OK)
{
setResult(RESULT_OK);
finish();
}
}
}
Finally in Activity C ,
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
setResult(RESULT_OK);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
You can override the functionality of back button in your Activity C
and exit from application, if it was pressed. Try this:-
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
//To Do - Exit App in your case
return true;
}
return super.onKeyDown(keyCode, event);
}
Hi you can add this code in your Activity which you want to exit
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit")
.setMessage("Are you sure you want to close this APP ")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
Are otherwise you may refer to this Post
it already has the post time:
http://developer.android.com/intl/es/reference/android/app/Activity.html
this.FinishAffinity();
finish your current_activity using method finish();
and then add below lines in onDestroy of the current_activity for Removing Force close
@Override
public void onDestroy()
{
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}