Bug 59816 - SetFlags crashes Application upon restart of the Activity in OnCreate
Summary: SetFlags crashes Application upon restart of the Activity in OnCreate
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.4.0
Hardware: PC Windows
: High normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-09-28 11:25 UTC by Rogier van der Hee
Modified: 2018-04-20 07:20 UTC (History)
6 users (show)

Tags: ac, fr
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Rogier van der Hee 2017-09-28 11:25:10 UTC
See PR 1151 (https://github.com/xamarin/Xamarin.Forms/pull/1151):

"The first call to the Forms.SetFlags must come before Forms.Init; otherwise it throws an InvalidOperationException. However, Android apps can restart (and call OnCreate again) due to configuration changes (e.g., plugging in a hardware keyboard). The second call to OnCreate (and subsequent second call to SetFlags) was triggering the InvalidOperationException after a configuration change and crashing the application.

This change ensures that only the first call to SetFlags must come before Init; subsequent calls are ignored."

Summary: using FastRenders on Android with 2.4.0 onwards is rather useless without the merge of this PR.
Comment 1 Michel Moorlag 2017-09-28 14:40:59 UTC
I am not sure if it is related but since I added 
"Forms.SetFlags("FastRenderers_Experimental");" to my Mainactivity.cs before the Forms.Init method I sometimes get the error :
 "Xamarin caused by: android.runtime.JavaProxyThrowable: System.InvalidOperationException: SetFlags must be called before Init".
It started in the 2.4 prerelease and is still the case in the stable release. 99 out of 100 starts it works fine but sometimes it crashes on startup.

Crashlog from hockeyapp:
Package: com.fullwood.crystalx3
Version Code: 1000
Version Name: 0.16.0
Android: 6.0
Android Build: MPI24.65-39-4
Manufacturer: motorola
Model: MotoE2(4G-LTE)
Thread: main-1
CrashReporter Key: 80114115-e946-4827-a065-830cb9df99ef
Start Date: 2017-09-28T13:07:43.113Z
Date: 2017-09-28T13:09:46.234Z
Format: Xamarin

Xamarin caused by: android.runtime.JavaProxyThrowable: System.InvalidOperationException: SetFlags must be called before Init
  at Xamarin.Forms.Forms.SetFlags (System.String[] flags) [0x0001b] in <7fcbd59ddd2f4ebfb60e22f10868ab72>:0 
  at CrystalLight.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00077] in <6fc8838ba53244818b3097705e4f1590>:0 
  at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <1e89c43d3c7d48b5ba69bfc0b1bdf114>:0 
  at (wrapper dynamic-method) System.Object:0e725cdc-81f7-4dab-bb23-f3409d615057 (intptr,intptr,intptr)
	at md543b4ad543ccc7fc7c0fdbc4da246c7e4.MainActivity.n_onCreate(Native Method)
	at md543b4ad543ccc7fc7c0fdbc4da246c7e4.MainActivity.onCreate(MainActivity.java:34)
	at android.app.Activity.performCreate(Activity.java:6245)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
	at android.app.ActivityThread.-wrap11(ActivityThread.java)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:148)
	at android.app.ActivityThread.main(ActivityThread.java:5443)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Comment 2 Emman Vazz 2017-09-28 15:57:55 UTC
I also get this crash randomly when we resume our droid app. I think that PR should have been merged in stable.
Comment 3 Samantha Houts [MSFT] 2017-09-29 19:48:04 UTC
You're correct! We missed the merge. This will be in 2.4.0-SR1, which will be available shortly. Thank you!
Comment 4 Bartłomiej Pater 2018-04-20 07:20:47 UTC
Hello. I'm still getting this exception in our app but only on Samsung SM-T580 with Android 7.0...

Crash from hockey app:

Xamarin caused by: android.runtime.JavaProxyThrowable: System.InvalidOperationException: SetFlags must be called before Init
Xamarin.Forms.Forms.SetFlags(string[] flags)<fa196d4afd1b4356b38d6cf3bb1e4df8>:0
Quality.Droid.MainActivity.OnCreate(Bundle bundle)<38b0cc63ff7e4317a352bb06694fdb78>:0
Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState)<a8f3095845e84f73b393b1adf986bb4a>:0
at (wrapper dynamic-method) System.Object.a4a1e72d-f4f3-4cfb-8cf8-9c0f09b53772(intptr,intptr,intptr)
md547801ebe9117933d17c05c88f35959d0.MainActivity.n_onCreate(Native Method)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
java.lang.reflect.Method.invoke(Native Method)

I'm using latest Forms ( and the funny case is that it doesn't crash when I just call


but since I had some bad experiences with fast renderers on Android <7.0 I have added a check for it:

double osVersion;
if (double.TryParse(App.OSVersion, out osVersion) && osVersion.CompareTo(7.0) <= 0)
    //enable fast renderers when we are sure to have 7.0+

and then it crashes. What could be the reason for this?