MediaPicker: Back button while using mediapicker (camera/file browser) closes app

Ruben Junius 21 Reputation points
2021-03-01T15:51:45.26+00:00

Hello everybody

We are creating an Xamarin Forms app, currently only for Android devices. In this app we're using the MediaPicker plugin to take a picture (MediaPicker.CapturePhotoAsync) and to load a picture from the phone's library (PickPhotoAsync). Our main view has two buttons on it. Each button uses one of the following MediaPicker methods: CapturePhotoAsync and PickPhotoAsync.

The button-clicked event looks like this:

73046-image.png
73033-image.png

This works. You can click the button and then pick a photo or take a new photo. However, when you are in the camera or file browser app, and you press the back button on the bottom of your screen, the app gets closed. I want the app to return to the main view, where the buttons are located. I don't see any options in the documentation that could help with this.

How can I return the my app, instead of closing it, when the back button is clicked while the camera app or photo picker app is open?

We navigate to the mainpage app from our login page using the following code:

73058-image.png

We currently use Xamarin.Essentials version 1.6.1, Xamarin Forms version 4.8.0.1821 and we deploy to an Android 9.0 (API 28).

Any help with this would be much appreciated.

Thanks in advance and kind regards

Ruben

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
0 comments No comments
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-03-02T02:59:11.987+00:00

    Hello,

    Welcome to Microsoft Q&A!

    However, when you are in the camera or file browser app, and you press the back button on the bottom of your screen, the app gets closed.

    Are you referring to the hardware back button ?

    73262-capture.png 73144-2capture.png

    Test with

    • Xamarin.Forms 5.0.0.2012/ 4.8.0.1821
    • Xamarin.Essentials 1.6.1
    • Android 9.0 (API 28) simulator

    Everything works as expected.

    when I click on the button , it returns the app(where the buttons locates) .

    Do you override the hardware back button clicking event in your project ?


    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.


0 additional answers

Sort by: Most helpful