2011-08-24 41 views
1

我有一個對話框,查看互聯網是否打開。如果它沒有打開,我會彈出一個窗口,讓您選擇打開或關閉應用程序。如果您選擇打開數據,它會將您帶到設置頁面打開數據。問題如下:當您從設置頁面向後按下時,對話框仍然存在,但不會被解除。這裏是代碼:Android:爲什麼這個對話框會彈出兩次?

public void calldialog() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage(
      "You need to enable an internet connection in order to use this Chirps:") 
      .setCancelable(true) 
      .setPositiveButton("Turn on Data", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 

          Intent newintent = new Intent(
            android.provider.Settings.ACTION_WIRELESS_SETTINGS); 
          newintent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
          startActivity(newintent); 
          dialog.dismiss(); 
         } 
        }) 
      .setNegativeButton("Exit",new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          Main.this.finish(); 
         } 
        }); 
    builder.show(); 

} 

當我只有一個選項,對話框正常工作。但只要我添加第二個選項,它會導致按鈕顯示兩次。包含calldialog()的網絡檢查函數在oncreate和onresume中被調用,這可能是問題的一部分。

回答

4

您在dialog.dismiss()之前調用startActivity()。這可以控制新的活動。

3

首先,當你開始你的應用程序在Android中activitysequens如下:

活動開始 - >onCreate()被稱爲 - >onResume()被稱爲 - >活動運行。

當您從設置返回到您的應用程序onResume()被調用時,所以您只需要在onResume()中進行檢查。

+0

這也有幫助,但另一個問題,我與我的問題有關:)。謝謝。 – Nick

+0

應該在onCreate()或onResume()中設置諸如Internet或Location之類的檢查嗎? – charany1