Android活动调用onCreate而不是onDestroy。为什么?

问题描述:

有意图触发的活动:设置,显示和隐藏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

这就是为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; 
    } 

}