2012-02-24 136 views
0

我想在Android中關閉總體應用程序時,我點擊對話框中的「否」按鈕。我已經使用了下面的代碼。如何殺死android應用程序中的所有活動?

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(); 
    } 

請任何一個告訴我如何解決這個問題。

+0

這裏是關閉所有活動的最佳解決方案: http://stackoverflow.com/a/5453228/1464582 – 2013-02-03 08:39:50

回答

5

您可以通過編程調用finish()方法上的活動,並呼籲主屏幕(模擬爲homeButton)是這樣的:

private void endApplication() { 
    Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    startActivity(intent); 
} 
+0

其工作,但是當我打開應用程序應用程序將顯示最後一個活動,即,我從應用程序退出的位置。 – NareshRavva 2012-02-24 10:24:53

0

你should't調用onDestroy()自己..改爲調用finish()關閉活動..

自行調用活動的生命週期方法是不好的做法(不知道如果可能的話)。他們是由Android操作系統本身來處理..

+0

thanq您的回覆,我想關閉整個應用程序,當我在「否」按鈕點擊 – NareshRavva 2012-02-24 06:27:26

+0

看到這個鏈接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 +接近+整個+申請+水溶液= F&AQI =&AQL =&gs_sm =&gs_upl =&BAV = on.2,or.r_gc.r_pw.r_qf。,cf.osb&FP = 70e2b85fee382b45&BIW = 1280&波黑= 933 – ngesh 2012-02-24 06:28:29

1

你應該殺死你的應用程序。你應該讓ActivityManager處理它。

特別是如果你希望用戶離開你的應用程序,然後送他們回家通過意向主屏幕。

8

讓simply.Suppose你有一個類Constants.java,你把所有的做到這一點有點您的重用application.In的常量聲明的活動堆棧是這樣的:

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); 

} 

,每當你創造一些活動,在它的OnCreate在最後一行你把這樣的事情:

Constants.addToActivityStack(this); 

現在定義的方法就像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 
} 

下列現在,當你需要完成你所有的活動,只需撥打Constants.killAllExcept(null)Constants.killAllExcept(this)如果你想保留本次活動。如果你想保持一個活動,但其他的/如果你願意,你可以完成這些工作全部

這種方法很方便。

0

應該強調的是,建議Constants.killAll()方法是不好的設計,並會如果使用不當,導致內存泄漏。保留對活動的靜態引用是Android中內存泄漏的最常見原因。

希望這會有所幫助。

相關問題