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

时间:2012-07-15 09:34:51

标签: android android-intent

有一个由意图触发的活动:设置,显示和隐藏ProgressDialog。当它设置并显示活动正常时,但是当调用hide时,调用onCreate而不是调用onDestroy方法。活动开始时,这是一个LogCat:

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

ProgressDialog已设置并显示在onNewIntent(Intent intent)方法中。但是当隐藏任务被称为

pd.dismiss();
finish();
调用

而不是调用onDestroy,调用onCreate:

HIDE_PROGESS
onPause
DialogHIDE
onResume
onPause
onCreate
onStart
onResume
onStop
destroyed
Activity -activityName- has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41347af0 that was originally added here android.view.WindowLeaked

没有ProgessDialog的白色显示。按下BACK按钮后

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 个答案:

答案 0 :(得分:0)

这就是onNewIntent()的文档中所说的内容:

  

在接收新意图之前,活动将始终暂停,因此您可以指望在此方法之后调用onResume()。

我相信您遇到的问题是由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;
    }

}