Xamarin.forms: [Android] Device.OpenUri("https://google.coom"); throws exception

Created on 3 Aug 2018  路  9Comments  路  Source: xamarin/Xamarin.Forms

Description

While calling Device.OpenUri("https://google.com"); on old versions of Android exception happens.
Exception:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.

Steps to Reproduce

  1. open Device.OpenUri("https://google.com"); on aAndroid API 21, 22 and 23, (Android 5.0-6.0)

Expected Behavior

Opens link in a browser

Actual Behavior

Exception throws

Basic Information

  • Version with issue:
  • Last known good version: 3.1.0.637273
  • IDE:
  • Platform Target Frameworks:

    • iOS:

    • Android:

    • UWP:

  • Android Support Library Version:
  • Nuget Packages:
  • Affected Devices:

Screenshots

Reproduction Link

1 low Android bug

All 9 comments

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.

Set this flag

Also provide the source where the problem is reproduced

I'm just calling this Device.OpenUri("https://google.coom"); code from my view model.
How can I add that flag while calling Device.OpenUri?

in manifest file on your android project

May I recommend: https://github.com/xamarin/essentials and our browser API : ) it actually uses chrome custom tabs too :)

Looking at this code: https://github.com/xamarin/Xamarin.Forms/blob/793bbe6c850c73fab21db372b80e52521c97f82f/Xamarin.Forms.Platform.Android/Forms.cs

It should have ClearTop and NewTask if you are using an application context: https://github.com/xamarin/Essentials/blob/master/Xamarin.Essentials/Browser/Browser.android.cs#L28-L30

If it was an Activity then this wouldn't be the case.

@xyashapyx What devices/emulators are you experiencing this crash on? Thanks!

I'm getting the same exception, when trying to open a website.

Part of the stack traces:

--- End of managed Android.Util.AndroidRuntimeException stack trace ---
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:912)
at android.app.ContextImpl.startActivity(ContextImpl.java:888)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:379)
at md51558244f76c53b6aeda52c8a337f2c37.InnerGestureListener.n_onSingleTapUp(Native Method)
at md51558244f76c53b6aeda52c8a337f2c37.InnerGestureListener.onSingleTapUp(InnerGestureListener.java:79)
at android.view.GestureDetector.onTouchEvent(GestureDetector.java:641)
at md51558244f76c53b6aeda52c8a337f2c37.Platform_DefaultRenderer.n_dispatchTouchEvent(Native Method)
at md51558244f76c53b6aeda52c8a337f2c37.Platform_DefaultRenderer.dispatchTouchEvent

I'm running this on:

Device: Google Pixel
OS: Android P 9
Xamarin.Forms ver. 3.1.0.697729

Keep calm and wait for merging the pull request

I've fixed that by using package that @jamesmontemagno provided.

Was this page helpful?
0 / 5 - 0 ratings