Reputation: 2106
When I open Android's default built-in browser it opens two tabs instead of just one. I have a MainPage, and a Page1. In MainPage I have a button that directs user to Page1.
Page1.xaml has nothing special as it is a default XAML template. Page1.xaml.cs:
public partial class Page1 : ContentPage
{
public Page1 ()
{
InitializeComponent ();
}
protected override void OnAppearing()
{
base.OnAppearing();
Device.OpenUri(new Uri("https://stackoverflow.com"));
}
}
Problem: When I enter Page1, the default Android browser(i.e Google Chrome) opens two tabs of the website https://stackoverflow.com instead of only one.
Update: As I debugged I noticed that this problem only occurs if we use Device.OpenUri in OnAppearing() method, as this won't occur if used in constructor.
Update: I tried to use native(Android for example) mechanism through DependancyService but this didn't work too:
var uri = Android.Net.Uri.Parse("https://stackoverflow.com");
var intent = new Intent(Intent.ActionView, uri );
intent.SetFlags(ActivityFlags.NewTask);
Application.Context.StartActivity(intent);
So I think this problem must be from Android's side not Xamarin.Forms.
Update: Strangely if I put a delay before opening browser as in the code below the problem no longer occurs:
protected override async void OnAppearing()
{
base.OnAppearing();
await Task.Delay(500);
Device.OpenUri(new Uri("https://stackoverflow.com"));
}
Though it might be a temporary workaround for this problem, I hope, however, someone finds a solution for this.
Upvotes: 0
Views: 192
Reputation: 2168
When I enter Page1, the default Android browser(i.e Google Chrome) opens two tabs of the website https://stackoverflow.com instead of only one.
I think the reason is that the OnAppearing()
fired again after Device.OpenUri()
get called. It should be called when you get back from the browser, but it is called earlier. The browser showed and Page1
OnDisappearing()
fired. After that, the second OnAppearing()
fired.
In your workaround, the second OnAppearing()
will be fired when you get back from the browser. but it will open the URI again.
I think you should move the code to other places, such as the button_Clicked
or Page1 ()
.
For example:
public Page1 ()
{
InitializeComponent ();
Device.OpenUri(new Uri("https://stackoverflow.com"));
}
Upvotes: 1