簡體   English   中英

onCreate在onActivityResult之前和之后調用

[英]onCreate called before and after onActivityResult

我嘗試以下方式打開相機:

...
    private void runCamera() {
        String storageState = Environment.getExternalStorageState();
        if (storageState.equals(Environment.MEDIA_MOUNTED)) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File imageFile = new File(Singleton.instanse.mPushFilePath);
            mImageFileUri = Uri.fromFile(imageFile);
            intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                    mImageFileUri);
            startActivityForResult(intent, CAMERA_RESULT);
    }
...

如果我運行此方法運行下一個方法:

07-16 19:46:22.264: I/System.out(6875): -onPause
07-16 19:46:26.104: I/System.out(6875): -onStop

我制作照片,結束下一步的方法:

07-16 19:46:41.217: I/System.out(6875): -onDestroy
07-16 19:46:41.284: I/System.out(6875): -onCreate
07-16 19:46:41.291: I/System.out(6875): -onStart
07-16 19:46:41.295: I/System.out(6875): -onActivityResult
07-16 19:46:41.295: I/System.out(6875): -onResume
07-16 19:46:41.295: I/System.out(6875): -onPostResume
07-16 19:46:41.522: I/System.out(6875): -onPause
07-16 19:46:41.522: I/System.out(6875): -onStop
07-16 19:46:41.522: I/System.out(6875): -onDestroy
07-16 19:46:41.604: I/System.out(6875): -onCreate
07-16 19:46:41.612: I/System.out(6875): -onStart
07-16 19:46:41.616: I/System.out(6875): -onResume
07-16 19:46:41.616: I/System.out(6875): -onPostResume

為什么onDestroy運行twiсe? 怎么解決? 在Android 2.2中發現此問題。 在Android 2.3.3 onDestroy中從未調用過!

實際上,相機會導致您的活動方向發生變化,這就是您的活動被銷毀和重新創建的原因。

在清單文件中添加它會阻止方向更改,並且您的活動不會被銷毀和重新創建。

<activity
    android:name=".YourActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="portrait" >
</activity>

通過意圖拍照后調用的活動已被殺死/ onCreate

不保證會調用onDestroy 確保在onPause而不是onStoponDestroy保存持久狀態。 你永遠不應該依賴onStoponDestroy來調用它。

onDestroy不保證在任何Android版本上執行,如果它,你應該快速做一些事情並返回

還看到這個Activity OnDestroy從未調用過?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM