Android: Activity getting Destroyed after calling

2019-01-03 06:15发布

I am having two Activities (A1 , A2). A1 calls A2 and from A2 i am calling the camera intent as below

launchIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
launchIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoPath);   
startActivityForResult(launchIntent,CAMERA_REQUEST);

It opens the camera, and i can take the picture. But the Problem arises once i click the save button (tick button in s3), my onActivityResult is not called instead A2's onDestroy method is called. I have few logics to be done in the onActivityResult fn.

I had read some post in Stackoverflow regarding this but i couldnt get useful output from that.I have my manifest like this for my second Activity(A2)

android:configChanges="keyboardHidden|orientation|locale"
android:screenOrientation="portrait

Note: In HTC One X my onActivityResult fn is getting called, but in my S3 second Activity(A2) is getting destroyed

Plz share ur thoughts on this. Thanks in Advance

6条回答
仙女界的扛把子
2楼-- · 2019-01-03 06:33

i had the same problem.i will be crazy but finally i have found a solution here. the issue is that when you click on "save" button of the camera the activity call change orientation method and it will destroy and recreated. try to set

android:configChanges="orientation|screenSize"

in android manifest (not only android:configChanges="orientation" because as suggest here, it not work for API level 13 or higher).

it prevent to destroy activity,it work for me.

查看更多
够拽才男人
3楼-- · 2019-01-03 06:43

Be sure you don't have the "Don't keep activities" Developer setting on, as it will destroy the activity you are leaving.

查看更多
我命由我不由天
4楼-- · 2019-01-03 06:43

My Activity has many tasks running when I am calling Camera Intent.. I have GPS location listeners, and many more tasks.

When it returns from the Camera Intent I need to add marker on a map in the location that the image was taken.. So if the user took more than one picture, it should show markers for each picture..

If it will destroy my Activity each time I call Camera Intent all the information that I had before about markers and their location will be lost.. And also, I will have to initialize the map again. That's a big problem for my app.

And yes, it only happens when I take a picture in Landscape mode. If I take the picture in portrait everything is fine so far..

I will try to use android:configChanges="orientation|screenSize" As suggested here.. Hope it will help!

查看更多
对你真心纯属浪费
5楼-- · 2019-01-03 06:45

Launching camera requires a lot of memory. So on devices with low memory android system closes the Activities running in background and hence onCreate() is called. Due to this photopath you have given becomes null and you wont be able to get the saved image.

Solution is to save the photopath while system is destroying your activity and then restore it again.

@Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub

            outState.putString("photopath", photopath);


        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey("photopath")) {
                photopath = savedInstanceState.getString("photopath");
            }
        }

        super.onRestoreInstanceState(savedInstanceState);
    }

and in case you are doing this on Fragment.

@Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub

            outState.putString("photopath", photopath));


        super.onSaveInstanceState(outState);
    }

    @Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey("photopath")) {
                photopath = savedInstanceState.getString("photopath");
            }
        }

        super.onViewStateRestored(savedInstanceState);
    }
查看更多
虎瘦雄心在
6楼-- · 2019-01-03 06:50

For me, it happens when i enable an option in Developer Mode called "Don't keep Activities". After i unchecked it, activity won't recreate while backing from choosing a picture from gallery or from camera app.

查看更多
来,给爷笑一个
7楼-- · 2019-01-03 06:53

The camera app requires a lot of memory and to free up memory, the operating system has to kill background apps, including yours. This is normal for all Android apps. Your activity will be recreated when the camera app returns. To retain activity state information, override onSaveInstanceState() to store your data and read them back in onCreate().

查看更多
登录 后发表回答