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.
Opens link in a browser
Exception throws
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.