Reputation: 67
In my project, I'm using a dependency service to turn on the location of the device. After turning on the location I need to modify the UI. I have added GPS status check codes on onAppearing(). The onAppearing method is triggering initially and checking the GPS status. If the GPS status is off a location sharing button will be available on the UI. If we tap on the share location button I will redirect the user to the device location setting page. When the user came back to the app after on the location, at that time onAppearing() is not firing.
My Code
protected override void OnAppearing()
{
base.OnAppearing();
ShareLocation();
}
public async void ShareLocation()
{
var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
if (status == PermissionStatus.Granted)
{
bool gpsStatus = DependencyService.Get<ILocSettings>().isGpsAvailable();
if (!gpsStatus)
{
location_sharing.IsVisible = true;
location_sharing_on.IsVisible = false;
}
else
{
location_sharing_on.IsVisible = true;
location_sharing.IsVisible = false;
}
}
}
Clicked Event for Button
var myAction = await DisplayAlert("Location", "Please turn on GPS to get your current location for emergency response.", "TURN ON", "CANCEL");
if (myAction)
{
DependencyService.Get<ILocSettings>().OpenSettings();
}
Dependancy service:
namespace projectName.Renderer
{
public interface ILocSettings
{
void OpenSettings();
bool isGpsAvailable();
}
}
[assembly: Dependency(typeof(LocationShare))]
namespace projectName.Droid.Services
{
public class LocationShare : ILocSettings
{
public bool isGpsAvailable()
{
bool value = false;
Android.Locations.LocationManager manager = (Android.Locations.LocationManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.LocationService);
if (!manager.IsProviderEnabled(Android.Locations.LocationManager.GpsProvider))
{
//gps disable
value = false;
}
else
{
//Gps enable
value = true;
}
return value;
}
public void OpenSettings()
{
Intent intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings);
intent.AddFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
}
Usually, the onAppearing() will always execute whenever the load a page. But I don't know what is the issue on this page. When loading the page after the location settings the onAppearing is not firing. I have attached a sample project here.
Upvotes: 1
Views: 2643
Reputation: 1404
'OnAppearing' method fired when you navigate from page and back to that page from another in stack. In your case, you put your application to sleep and when you resume the app then OnAppearing event will not call because page has not appeared though it was already there.
You can use MessagingCenter to achieve your goal.
App.xaml.cs
protected override void OnResume()
{
MessagingCenter.Send(this, "ShareLocation");
}
MainPage.xaml.cs
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<App>(this, "ShareLocation", (sender) =>
{
ShareLocation();
});
ShareLocation();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<App>(this, "ShareLocation");
}
Upvotes: 3