how to kill all activities in android application?

2019-09-09 05:01发布

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.

5条回答
Summer. ? 凉城
2楼-- · 2019-09-09 05:19

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);
}
查看更多
一夜七次
3楼-- · 2019-09-09 05:21

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.

查看更多
Deceive 欺骗
4楼-- · 2019-09-09 05:25

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..

查看更多
走好不送
5楼-- · 2019-09-09 05:26

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.

查看更多
疯言疯语
6楼-- · 2019-09-09 05:27

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.

查看更多
登录 后发表回答