之前和onActivityResult后的onCreate称为onActivityResult、onCreate

2023-09-12 04:30:20 作者:無力改變

我试图打开摄像头以下方式:

I try open camera following way:

...
    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);
    }
...

如果我运行此方法运行下一个方法:

if I run this method run next methods:

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

我做的照片,运行完下一个方法:

I make photo, end run next methods:

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的发现。在安卓2.3.3的onDestroy从来没有所谓!

Why onDestroy run twiсe? How to fix it? This problem found in Android 2.2. In Android 2.3.3 onDestroy never called!

推荐答案

其实,相机会导致方向的改变你的活动,这就是为什么你的活动被破坏并重新创建。

Actually the camera causes the orientation change in your activity that is why your activity is being destroyed and recreated.

添加这在你的manifest文件,将prevent的方向变化,你的活动将不会被破坏并重新创建。

Add this in your manifest file it will prevent the orientation change and your activity will not get destroyed and recreated.

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

Activity通过意向拍照