5

I have my activity which uses user defined objects to keep track of progress. I need to fetch an image from camera when user presses a button and store it in one of these objects.

The problem is that sometimes it works fine but many times it re-initializes my activity and hence all the objects and my progress is lost. As far as i understand it might be because camera is a memory intensive app and while in background, the OS destroys my app to free memory.

I would prefer not to make my objects Parcable and then save them to the Bundle.

I have modified my manifest for android so that activity tag include

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

Here is my camera intent

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);

Here is onActivityResult method

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK )
        modifyResponseView_image();
}

I have tried many more suggestions and feel that keeping my activity in foreground might help but am not sure about if it will nor how to do it.

Any help you can provide will be deeply appreciated. Alternatives to apply this functionality will also help.

Anubhav
  • 933
  • 1
  • 7
  • 10
  • 2
    Saving the state in a Bundle passed through onPause is the recommended best practice. If you don't want to make your objects Parcelable, you can just pass the objects variables. – Mike Ortiz Jan 04 '14 at 22:39
  • please post the stacktrace from logcat too. See if onDestroy() is called on the activity. Find the reason using isFinishing() http://goo.gl/gdxJgo – Binoy Babu Jan 04 '14 at 22:39
  • I solved it by creating a camera activity with basic functionality. Not only did it solve the problem it also helped getting results faster than invoking the camera app. – Anubhav Jan 05 '14 at 19:20
  • possible duplicate of [Android: Activity getting Destroyed after calling Camera Intent](http://stackoverflow.com/questions/16014930/android-activity-getting-destroyed-after-calling-camera-intent) – Alex Cohn Sep 29 '15 at 20:20

1 Answers1

9

The problem is that sometimes it works fine but many times it re-initializes my activity and hence all the objects and my progress is lost.

Your activity will be destroyed in other conditions as well, such as if the user rotates the screen, changes locale, puts their device into a dock, removes their device from a dock, etc. In any of those cases, your activity's data will be lost, unless you are retaining it by some means (e.g., savedInstanceState Bundle).

As far as i understand it might be because camera is a memory intensive app and while in background, the OS destroys my app to free memory.

Correct. In my previous paragraph, I listed conditions for configuration changes, in which case your process sticks around but your activity is destroyed and recreated. If the OS terminates your process, everything you have in memory is gone. The savedInstanceState Bundle should be handed back to your activity, though, as that is passed across process boundaries to be held in the OS until such time as control returns to your app.

I would prefer not to make my objects Parcable and then save them to the Bundle.

Then save your data to a file.

Or, save your data to a database.

Or, save your data to SharedPreferences.

Or, save your data to "the cloud".

Or, do not use a third-party camera app, and integrate the camera directly in your application.

I have modified my manifest for android so that activity tag include

That hasn't been a particularly useful combination of attributes in nearly three years:

  • It ignores all other configuration changes

  • It does not properly handle orientation changes

  • It is generally an anti-pattern

feel that keeping my activity in foreground might help but am not sure about if it will nor how to do it

If your activity is in the foreground, then the user cannot use the camera app, since the camera app will not be in the foreground.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for context wise explaination – Anubhav Jan 05 '14 at 09:23
  • I have the same problem and I tried implementing onSaveInstanceState in both my fragment and my activity, but the method never gets called. After taking a picture and pressing save, the activity's onDestroy method gets called directly and I can't do anything... Any suggestions? – Sandra Jan 25 '16 at 09:31
  • @Sandra: I suggest that you ask a fresh Stack Overflow question, where you can provide your code and explain your symptoms in greater detail. – CommonsWare Jan 25 '16 at 09:37
  • The problem that I am facing is already explained here: http://stackoverflow.com/questions/16014930/android-activity-getting-destroyed-after-calling-camera-intent, but the solution provided (the first answer) does not seem like the right way to solve the problem to me. That is why I wanted to try with onSaveInstanceState, which is also mentioned in the thread whose link I provided – Sandra Jan 25 '16 at 09:44