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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With