Share via

Cannot use CapturePhotoAsync() if camera causes app to background and restore?

JRE 1 Reputation point
2021-07-29T09:14:55.98+00:00

I understand that during the lifecycle of the app, an activity may be put into the background, and essentially be 'put to sleep'. I have code during a normal activity restore, that will recover the data etc.

When I use a method like:

private async void TakePhoto()
    {
        // Photo requested

        var photo = await MediaPicker.CapturePhotoAsync();

       // Photo taken

    }

If when calling TakePhoto(), the Android device does not fully background the app, the method successfully hits "Photo taken". If the app is fully backgrounded (as soon as the camera app loads in front it decides to free up resources), once the photo is taken, the activity restores itself, but the original method is no longer executing, so I am unable to continue to get the photo response, thus "Photo taken" is never hit.

So my question is, how am I implementing this wrong, given how the Android activity lifecycle works?

Developer technologies | .NET | Xamarin

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,971 Reputation points
    2021-08-02T01:51:49.787+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If I have a device where Android decides to free up the resource, as the camera is launched, and my app is now suddenly put into the background, this is a problem...

    Do you face the issue or just want to avoid this problem? When the MediaPicker.CapturePhotoAsync is called, it will start the system internal camera api/function instead of starting the camera application. You could click the 'progress' button to check the progress of the application of the device after calling the command. You'll see that there is only one progress, the capture function runs in your application's progess. So when your application is killed by the system or manaually, the capture function will also be killed.

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.