0

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.

NareshRavva
  • 823
  • 3
  • 21
  • 50

5 Answers5

8

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:

public static ArrayList<WeakReference<Activity>> activity_stack=new ArrayList<WeakReference<Activity>>();
/**
 * Add the activity as weak reference to activity stack.
 * @param act
 */
public static void addToActivityStack(Activity act)
{
    WeakReference<Activity> ref = new WeakReference<Activity>(act);
    activity_stack.add(ref);

}

And whenever you create some activity,in it's onCreate at the last line you put something like this:

Constants.addToActivityStack(this);

Now define a method like following in Constants.java

    /**
 * Kill all the activities on activity stack except act.
 * To kill all the passed parameter should be null.
 */
public static void killAllExcept(Activity act)
{
    for(WeakReference<Activity> ref:Global.activity_stack)
    {
        if(ref != null && ref.get() != null)
        {
            if(act != null && ref.get().equals(act)) 
            {
                continue;//dont finish this up.
            }
            ref.get().finish();
        }
    }
    activity_stack.clear();//but clear all the activity references
}

Now,when you need to finish up all your activities,just call Constants.killAllExcept(null) or Constants.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.

Munim
  • 2,626
  • 1
  • 19
  • 28
5

you can call the finish() method on your activity and call the homescreen (simulate the homebutton) programmatically like this:

private void endApplication() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}
herom
  • 2,532
  • 29
  • 41
1

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.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

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.

0

You should't call onDestroy() yourself.. instead call finish() 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..

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • thanq for your response i want to close entire app when i click on "no" button – NareshRavva Feb 24 '12 at 06:27
  • see this link http://www.google.co.in/#hl=en&gs_nf=1&cp=31&gs_id=3w&xhr=t&q=how+to+close+whole+application&pf=p&sclient=psy-ab&pbx=1&oq=how+to+close+whole+application+&aq=f&aqi=&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=70e2b85fee382b45&biw=1280&bih=933 – ngesh Feb 24 '12 at 06:28