I have
public class MyApp : Application
In Java I would add a line to the manifest and pass it the namespace and name of my application:
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:name="com.devahead.extendingandroidapplication.MyApplication">
In Xamarin, there is the [Application]
attribute but the documentation states that the Name
member is not supported. So how do I get my subclass registered? Where to apply the attribute to?
If I add the attribute to my subclass, I get:
System.NotSupportedException: Unable to activate instance of type TestClient_Android.MyApplication from native handle 14d00019
Found it. The documentation is outdated. You will need a special c'tor with two parameters and you will have to add the [Application]
attribute:
[Application] public class MyApplication : Application { public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip) { } }
EDIT: In addition it seems one has to override OnCreate(). If you have only the constructor, it will not be called.
public override void OnCreate() { // If OnCreate is overridden, the overridden c'tor will also be called. base.OnCreate(); }
EDIT November 2015: here's a link to the Xamarin documentation which explains why this behavior exists.
...Xamarin.Android hooks into this by adding a mono.MonoRuntimeProvider ContentProvider to AndroidManifest.xml during the build process. The mono.MonoRuntimeProvider.attachInfo() method is responsible for loading the Mono runtime into the process. Any attempts to use Mono prior to this point will fail. ( Note: This is why types which subclass Android.App.Application need to provide an (IntPtr, JniHandleOwnership) constructor, as the Application instance is created before Mono can be initialized.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With