Home » Android » android – Opening the camera in a different process

android – Opening the camera in a different process

Posted by: admin May 14, 2020 Leave a comment


One of my apps has a serious activity recreation issue in some Kitkat Samsung devices. The problem appears more frequently when taking pictures, and it isn’t observed in crappy Jellybean Samsungs with half the RAM. Logs show a low memory condition, although no OutOfMemoryExceptions are being thrown. I think there’s a more aggressive activity closing policy in Kitkat (or else the default camera app is leaky).

I was wondering whether the OS closes my activities when my app’s memory is high, or instead it closes them when the total memory used by all apps is high. If the OS had a per-process threshold, then maybe opening the camera in a different process might help.

I know it is possible to start a service in its own process using the process attribute in the manifest. Assuming no equivalent attribute exists for Activities, if I started a proxy service in its own process and then started an activity from this service, would the activity run in this process as well?

How to&Answers:

The behaviour described is totally agreed with the Application fundamentals documentation from which it is clear that “opening the camera in a different process” won’t help you, just because this is what already happens by default:

When the system starts a component, it starts the process for that app (if it’s not already running) and instantiates the classes needed for the component. For example, if your app starts the activity in the camera app that captures a photo, that activity runs in the process that belongs to the camera app, not in your app’s process.

So when the camera app is brought to foreground, your app goes to background, which makes it be one of the candidates to be killed by the system when it is low on memory as per Processes and Application Life Cycle. You’re witnessing a normal OS’s behaviour. Total amount of RAM isn’t a key point. Free memory of the amount available for the process in foreground matters.

With that said, the answer to your question is No. Any Activity starts and runs within the application (process) it was originally declared in through the manifest file, no matter which process the start request comes from.

The command

adb shell ps | grep 'app_name'

will tell you which processes your and camera app are running on.

Two apps can run in the same process though, but the necessary prerequisite for this is the same signature of the apps… From Application fundamentals:

It’s possible to arrange for two apps to share the same Linux user ID… Apps with the same user ID can also arrange to run in the same Linux process and share the same VM (the apps must also be signed with the same certificate).