Alon Gubkin Alon Gubkin - 1 year ago 62
Android Question

PhoneGap camera restarts the application

I'm using PhoneGap's
function to retrieve a photo from the device's camera on Android.

function onSuccess(imageData) {

function onFail(message) {
alert('Failed because: ' + message);

$(function() {
$("button").tap(function() {, onFail, { quality: 50 });

When I click the button, it does start the camera, but when I click OK on the camera app after taking a photo, it restarts the application.

I tried to:

  • use different source types.

  • use different destination types.

  • reduce quality.

Any ideas?

EDIT: I also started an issue at github.

Answer Source

This problem isn't actually about Phonegap. It's a common issue on native android apps too.

It occurs because when the camera is triggered, the android activity goes background (onStop state), waiting for the camera to take the picture. Then the GC comes and kills the activity to free memory before the conclusion of camera action, and when the camera is done your activity has already died. That is why the app is restarted.

It's on Android Lifecycle docs ( ):

If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.

The same occurs by acessing the media gallery or other resources that causes your activity to go background. Phonegap (now Cordova) team is already working to improve this ( ).

We had this problem in our company and the solution was to natively develop a Phonegap plugin to use our customized camera, then our activity never go to onStop state. Follow the android API instructions on and try it too.

See ya!

Edit 1:

We submited a Google Code project named Foreground Camera Plugin that fixes the problem of Android Camera restarting Phonegap applications. There is some orientation on how to use it too. Please see:

Edit 2:

Since this problem happens with gallery too, we submited another Google Code project named Foreground Gallery Plugin that works with Cordova and fixes this issue. Please see: