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

Samo Simončič 1 Reputation point
2021-10-05T12:20:42.993+00:00

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);
            }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points
    2021-10-06T06:03:25.827+00:00

    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.

    0 comments No comments