2012-07-15 49 views
1

有意圖觸發的活動:設置,顯示和隱藏ProgressDialog。當它設置並顯示活動正常工作時,但是當調用hide時,不是調用onDestroy方法,而是調用onCreate。這裏是一個的logcat當活動開始:Android活動調用onCreate而不是onDestroy。爲什麼?

SHOW_PROGESS 
SET_PROGESS 
onCreate 
onStart 
onResume 
SET_PROGESS 
onPause 
DialogSET 15 
onResume 
SET_PROGESS 
onPause 
DialogSET 16 
onResume 
... 

的ProgressDialog被設定並在onNewIntent(意向意圖)所示的方法。但是,當隱藏任務被稱爲

pd.dismiss(); 
finish(); 

被調用,而不是調用的onDestroy的的onCreate被稱爲:

HIDE_PROGESS 
onPause 
DialogHIDE 
onResume 
onPause 
onCreate 
onStart 
onResume 
onStop 
destroyed 
Activity -activityName- has leaked window [email protected] that was originally added here android.view.WindowLeaked 

有白色顯示沒有ProgessDialog。按下後退按鈕後,

onPause 
onDestroy 

被調用,然後我可以看到我想要的掃管笏。我該如何解決這個問題,我不應該按下BACK按鈕來再次調用onDestroy?

的意圖開始是這樣的:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
       intent.setAction(intent.ACTION_VIEW); 

       startActivity(intent); 

謝謝!

編輯:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);      

     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

     extras = getIntent().getExtras(); 

     progress = 0; 
     max = extras.getInt("max"); 
     title = extras.getInt("title"); 

     pd = new ProgressDialog(this); 

     pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     pd.setCancelable(false); 

     Log.w("screenPD", "onCreate"); 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 

     setIntent(intent); 

     int newTitle = intent.getExtras().getInt("title"); 

     if (intent.getExtras().getString("action").equals("set")){ 
      pd.setTitle(newTitle); 
      pd.setMessage(intent.getExtras().getString("message")); 
      max = intent.getExtras().getInt("max"); 
      pd.setMax(max); 
      pd.setProgress(intent.getExtras().getInt("progress")); 
      pd.show(); 

      Log.e("DialogSET", "DialogSET "+intent.getExtras().getInt("progress")); 
     } 
     else if (intent.getExtras().getString("action").equals("show")){ 
      pd.setProgress(intent.getExtras().getInt("progress")); 
      pd.setMessage(intent.getExtras().getString("message")); 
      //pd.show(); 

      Log.e("DialogSHOW", "DialogSHOW "+progress); 

     } 
     else if (intent.getExtras().getString("action").equals("hide")){ 

      //pd.dismiss(); 
      finish(); 

      Log.e("DialogHIDE", "DialogHIDE"); 

     } 
    } 
+1

發佈您的代碼。 – 2012-07-15 09:36:23

+0

我發佈了我的代碼 – Alex 2012-07-15 09:46:14

回答

0

這就是爲onNewIntent()文檔上說:

的活動總是會接收到一個新的意圖前停了下來,這樣你就可以的onResume COUNT()在這個方法之後被調用。

我相信你所看到的問題是由於finish()呼叫onNewIntent()正在進行,然後框架(看到這需要恢復您的活動作爲下一步)再次復活你的活動。

嘗試在onNewIntent()移動你的代碼,而不是onResume()(也許把一個標誌太,檢查是否需要處理),這樣的:

private boolean mNewIntentProcessed; 

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    setIntent(intent); 

    mNewIntentProcessed = false; 

    // move the rest of the code here to onResume() .. 
} 


@Override 
protected void onResume() { 
    super.onResume(); 

    if (!mNewIntentProcessed) { 

     final int newTitle = intent.getExtras().getInt("title"); 

     // the rest of your code from onNewIntent() should be moved here .. 

     mNewIntentProcessed = true; 
    } 

} 
相關問題