I want to close total application in android when i click on "NO" Button in Dialog. i have used the following code.
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setTitle("GASIMIZER");
builder1.setCancelable(false)
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent i = new Intent(Finalpage.this,NewproActivity.class);
startActivity(i);
}
})
.setNegativeButton("NO",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
quit();
}
});
AlertDialog alert1 = builder1.create();
alert1.show();
break;
}
return null;
}
public void quit() {
onDestroy();
}
please any one tell me how can i solve this problem.
you can call the
finish()
method on your activity and call the homescreen (simulate the homebutton) programmatically like this:You should NOT kill your applications. You should let the ActivityManager handle that.
Specifically if you want the user to leave your application, then send them home via an Intent to the homescreen.
You should't call
onDestroy()
yourself.. instead callfinish()
to close the Activity..Calling Activity life cycle method by yourself is bad practice (Don't know if its possible). They are handled by Android OS itself..
It should be highlighted that the suggested Constants.killAll() approach is bad design and will if used incorrectly lead to memory leaks. Preserving static reference to an activity is the most common cause of memory leaks in Android.
Hope this helps.
Let's do this a bit simply.Suppose you have a class
Constants.java
where you put all your reused constants of the application.In that declare an activity stack like this:And whenever you create some activity,in it's onCreate at the last line you put something like this:
Now define a method like following in
Constants.java
Now,when you need to finish up all your activities,just call
Constants.killAllExcept(null)
orConstants.killAllExcept(this)
if you want to keep this activity.This method is convenient when you want to keep one activity but the others/if you want you can finish them up all.