1

I have spent a day dealing with this problem. At the beginning I have a lot of leaks, I solved them but still not satisfied by the result.
The task is simple to start camera, take photo, save it to the specified path and get result in activity if photo has been taken successfully.
Looks very simple task.
Let me describe my sequence of execution.
I have list of items, when I click on any item fullscreen dialog appears then via callback activity is notified that user wants to take a photo and starts camera activity by the intent, takes the photo and returns the result of calling activity.
There are some snippets of code .

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.e("ON ACTIVITY CREATE !!");
            setContentView(R.layout.my_layout); 
         }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            Log.e("ON ACTIVITY RESULT !!");
            if (requestCode == PHOTO_REQUEST) {
                if (resultCode == RESULT_OK) {
                    mAfterTakingPhoto = true;
                    mPreferences.edit().putBoolean("after_photo", mAfterTakingPhoto).apply();
                }
            }
        }
            @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            Log.e("ON RESTORE INSTANCE");
            mImagePath = savedInstanceState.getString("image_path");
            Log.e("RESTORED VALUES " + mImagePath  + " " + mAfterTakingPhoto);
        }

        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            Log.e("ON SAVE INSTANCE");
            outState.putString("image_path", mImagePath);
            Log.e("SAVING VALUES " + mImagePath  + " " + mAfterTakingPhoto);
        }

        @Override
        protected void onResume() {
            super.onResume();
            Log.e("ON RESUME !!!" + " mAfterTaking a photo " + mAfterTakingPhoto);
            mAfterTakingPhoto = mPreferences.getBoolean("after_photo", false);
        }
   @Override
    protected void onDestroy() {
        Log.e("ON ACTIVITY DESTROY");
        // Do some cleanup stuff
        super.onDestroy();
    }

And starting activity for taking photo (callback for a dialog)

@Override
    public void onTakePhotoRequest() {
            mImagePath = PHOTO_DIR.getAbsolutePath() + File.separator + filename;
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mImagePath));
            startActivityForResult(cameraIntent, PHOTO_REQUEST);
    }   

Sometimes it works great ,when calling activity is alive, but when it is destroyed something crazy happens.

Here is the log of success execution.

    com.example.app E/Log﹕ --ON RESUME !!! mAfterTaking a photo false
com.example.app E/Log﹕ --ON SAVE INSTANCE
com.example.app E/Log﹕ --List item count is : 60
com.example.app E/Log﹕ --Adapter item count is : 60
com.example.app E/Log﹕ --SAVING VALUES /mnt/sdcard/Pictures/photos/08_17_2015 02_28_02.jpg 32 false
com.example.app E/Log﹕ --ON ACTIVITY RESULT !!
com.example.app E/Log﹕ --ON RESUME !!! mAfterTaking a photo true

And this is weird execution log.

com.example.app E/Log﹕ --ON ACTIVITY CREATE !!
com.example.app E/Log﹕ --ON RESUME !!! mAfterTaking a photo false
com.example.app E/Log﹕ --List item count is : 60
com.example.app E/Log﹕ --Adapter item count is : 60
com.example.app E/Log﹕ --ON SAVE INSTANCE
com.example.app E/Log﹕ --SAVING VALUES /storage/emulated/0/Pictures/photos/08_17_2015 02_45_18.jpg 10 false
com.example.app E/Log﹕ --ON ACTIVITY DESTROY
com.example.app E/Log﹕ --ON ACTIVITY CREATE !!
com.example.app E/Log﹕ --ON RESTORE INSTANCE
com.example.app E/Log﹕ --RESTORED VALUES /storage/emulated/0/Pictures/photos/08_17_2015 02_45_18.jpg 10 false
com.example.app E/Log﹕ --ON RESUME !!! mAfterTaking a photo false
com.example.app E/Log﹕ --ON ACTIVITY RESULT !!
com.example.app E/Log﹕ --ON RESUME !!! mAfterTaking a photo true
com.example.app E/Log﹕ --List item count is : 60
com.example.app E/Log﹕ --Adapter item count is : 60
com.example.app E/Log﹕ --List item count is : 60
com.example.app E/Log﹕ --Adapter item count is : 60
com.example.app E/Log﹕ --List item count is : 60
com.example.app E/Log﹕ --Adapter item count is : 60
com.example.app E/Log﹕ --ON SAVE INSTANCE
com.example.app E/Log﹕ --SAVING VALUES /storage/emulated/0/Pictures/photos/08_17_2015 02_45_18.jpg 10 false
com.example.app E/Log﹕ --ON ACTIVITY DESTROY
com.example.app E/Log﹕ --ON ACTIVITY CREATE !!
com.example.app E/Log﹕ --ON RESTORE INSTANCE
com.example.app E/Log﹕ --RESTORED VALUES /storage/emulated/0/Pictures/photos/08_17_2015 02_45_18.jpg 10 false
com.example.app E/Log﹕ --ON RESUME !!! mAfterTaking a photo false

As you can see activity is destroyed than created again,restored, onActivityResult and so on, but then suddenly it is destroyed again and recreated but without onActivityResult callback, and it makes screen black for a several seconds.
Furthermore, list seems to be recreated three times and then it crashes.
Interesting thing that activity is destroyed not in background while taking a photo, but on photo button click(camera activity is going to clos). I have tried everything that I know, I have disabled all 3rd-party libs, and make this activity inherit SherlockFragmentActivity class. So the problem is somewhere here, but I have no idea know, tried almost everything.
Please help with this problem, what is wrong here or maybe this bug. I have no idea what to try more.

P.S. BTW I am using Genymotion Emulator Custom Phone, I have tried another Sony Xperia Tablet there is no such behaviour.

CROSP
  • 4,499
  • 4
  • 38
  • 89
  • 1
    Did you get any further with this? I also have similar problems with activity lifecycle after camera intent. My activity is getting re-created twice, and it's causing race condition havok with my code. Sometimes it works, sometimes it doesn't. –  Oct 09 '15 at 16:01
  • 8
    After a long time of troubleshooting this, it looks like a solution for you could be http://stackoverflow.com/questions/10411009/activity-killed-oncreate-called-after-taking-picture-via-intent which would make your question a duplicate. The `android:configChanges="orientation|keyboardHidden|screenSize"` line is what (so far) seems to be fixing the problem in my case. –  Oct 09 '15 at 16:17

0 Answers0