Android: Activity Getting Destroyed After Calling Camera Intent

Camera Intent destroys Activity

I think it's the android:noHistory="true"... Looks like after taking the photo it returns to the previous activity on the history stack but there is no activity in there as it is set to keep no history.

Android activity destroyed when returns from camera app through intent


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.

Activity killed / onCreate called after taking picture via intent

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

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|screenSize"
android:screenOrientation="portrait" >
</activity>

Activity gets killed while executing the camera intent

OK, I found the problem. The issue was not so much that the activity was being killed, but that it was not being restored. I realized that this can happen when your activity is declared in the manifest with android:noHistory="true". So the activity was removed from the stack upon being killed, and the previous activity was shown instead.

After I removed the noHistory parameter, the activity started appearing again after using the camera.

ACTION_IMAGE_CAPTURE intent: Avoid Activity being destroyed / process being killed


Almost always the whole app process will be killed while the user is in the separate camera Activity/App

This is not surprising. A camera app can consume quite a bit of memory, so Android needs to free up memory by terminating background apps' processes.

After all, the ActivityManager makes a wrong decision to kill the app

Given that a likely alternative is the OS crashing, I suspect the user would agree with the OS decision to terminate your process.

Having to reload the models so frequently takes up a lot of CPU and memory resources.

Then perhaps you should not be starting another app from yours. Take the photo yourself. Use the camera APIs directly, or use libraries like Fotoapparat and CameraKit-Android as simpler wrappers around those APIs.

The only way I can think of to 'fix' this issue is to implement my own camera Activity inside the app, but this goes counter Android's own best practices.

By that argument, no devices would ever have a camera app, as writing any camera app "goes counter Android's own best practices".

Any app that needs a camera must use the camera APIs (directly or indirectly) to have any shot at reliable behavior. You are assuming that thousands of camera apps are all properly written and will correctly honor your ACTION_IMAGE_CAPTURE Intent (e.g., putting the results in the place that you designate with EXTRA_OUTPUT). Many camera apps have buggy ACTION_IMAGE_CAPTURE implementations. ACTION_IMAGE_CAPTURE is not unreasonable for cases where you and the user can live without the picture being taken (e.g., a note-taker app that has an "attach photo" feature), but that would not seem to be the case with your app.

Camera or Gallery intent destroys old activity on some devices

It is normally, that Android kills your Activity when other app runs.

You must save Activity state in onSaveInstanceState and when activity will be recreated restore state in onRestoreInstanceState or in onCreate.

To restore state of WebView you may use cookies and sessions and save last opened url. When activity will be recreated just navigate WebView last saved url and process result from camera.



Related Topics



Leave a reply



Submit