question

SamoSimoni-6501 avatar image
0 Votes"
SamoSimoni-6501 asked KyleWang-MSFT answered

How to prevent that the Pop-up message reloads the current content page

If the location on the Android phone is not turn on, the native code called by the Xamarin.forms is called which shows the Pop-up for enabling location. This causes that the actual Xamarin content page goes to the background and consequently OnDisappiring event is called. Is it possible to prevent this behavior? I do not want that showing the PopUp implies that the OnDisappring event is called. Here is my code:

 try
             {               
                   
                LocationRequest locationRequest = LocationRequest.Create().SetPriority(LocationRequest.PriorityHighAccuracy).SetInterval(60 * 1000 * 5).SetFastestInterval(60 * 1000 * 2);
    
                LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest).SetNeedBle(true);
    
                var result = await LocationServices.GetSettingsClient(MainActivity.CurrentActivity).CheckLocationSettingsAsync(builder.Build());
                                    
             }
             catch(ResolvableApiException ex)
             {
                   
                 ResolvableApiException resolvable = (ResolvableApiException)ex;
                 resolvable.StartResolutionForResult(MainActivity.CurrentActivity, 0x1);
             }


dotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered

Hi SamoSimoni-6501,

Welcome to our Microsoft Q&A platform!

It's a known issue which has been reported on GitHub, you can triage it in the link: https://github.com/xamarin/Xamarin.Forms/projects/3#card-54694446

To handle this issue, here are some workarounds you can try.

If it's a single page, just set the MainPage in App.xaml.cs as follows.

 MainPage = new MainPage(); // don't use "new NavigationPage()"

If it's not a single page and you need to use NavigationPage to push new pages. Try to use Xamarin.Essentials: Preferences to record if permission requested.

Here is a simple demo you can refer to.

 bool showNewPage = false;
    
 // button to get location(permission request pop up)
 private async void Button_Clicked(object sender, EventArgs e)
 {
     Preferences.Set("permission_request_popup", true);
     showNewPage = false;
    
     Console.WriteLine("click");
     try
     {
         var location = await Geolocation.GetLastKnownLocationAsync();
    
         if (location != null)
         {
             Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
    
 }
    
 protected override void OnDisappearing()
 {
     bool key = Preferences.Get("permission_request_popup", false);
     if (key == true && showNewPage == false)
         return;
    
     base.OnDisappearing();
     Console.WriteLine("Disappearing");
 }
    
    
 // button to push new page
 private async void Button_Clicked_1(object sender, EventArgs e)
 {
     showNewPage = true;
     await Navigation.PushAsync(new Page1());
 }

Regards,
Kyle


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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.