Reputation: 803
OnAppearing
Called at wrong page after close browser Browser.OpenAsync
I would like to describe the scenario :
I have 2 pages
Main Home.xaml
Sub ItemDetailPage.xaml
When I Select an item from List in Home.xaml
I do the following:
async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
{
var item = args.SelectedItem as SelectedItem;
if (item == null)
return;
await Shell.Current.GoToAsync($"{nameof(ItemDetailPage)}?ItemId={item.Id}");
((ListView)sender).SelectedItem = null;
}
In the page ItemDetailPage.xaml
I clicked GoToLink
method
private async void GoToLink(object sender, EventArgs e)
{
await Browser.OpenAsync($"https://website.com/");
}
The browser opened without any issue until I close it the code will call OnAppearing()
related to Home.xaml
page and keep ItemDetailPage.xaml
page focused, Then I press back in ItemDetailPage.xaml
await Shell.Current.GoToAsync("..");
The screen back to Home.xaml
page but without calling OnAppearing()
Upvotes: 1
Views: 998
Reputation: 803
I do workaround for this problem
by using MessagingCenter
AS following:
After close ItemDetailPage.xaml
the only event occur in this page was OnDisappearing()
So before opened the browser I do create a flag IsBrowserOpened
(Static variable)
ItemDetailPage.xaml
:
private async void GoToLink(object sender, EventArgs e)
{
IsBrowserOpened= true;
await Browser.OpenAsync($"https://website.com/");
}
protected override void OnDisappearing()
{
base.OnDisappearing();
if (IsBrowserOpened)
{
MessagingCenter.Send(this, "updateFromBrowser", 1);
}
}
Home.xaml
Page
public partial class Home : ContentPage
{
public Home()
{
InitializeComponent();
MessagingCenter.Subscribe<ItemDetailPage, int>(this, "updateFromBrowser", async (obj, item) =>
{
// Update home page for incoming new data
});
}
}
Upvotes: 2