I need to perform some actions before application actually started.
I tried this but can't reach breakpoint:
public class MyApplication : Android.App.Application {
public MyApplication(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) { }
public override void OnCreate() {
base.OnCreate();
int test = 1; //breakpoint
}
}
Am I doing something wrong? Or here is some debugger problem?
The
Xamarin.Android
way of add your own Android Application class:1) Specific the class
Name
:2) Update your manifest:
Add this fully qualified name as a
android:name=
attribute:The key item here is to "Register" your custom Application class by using the
[Android.App.Application]
attribute. Secondly you need to provide anOnCreate
override to ensure the custom application class's constructor is invoked. Otherwise without the[Application]
attribute, we do not register the custom<application>
element in theAndroidManifest.xml
. Rather we use the defaultandroid.app.Application
instead:<application android:label="App6" android:name="android.app.Application" android:allowBackup="true" android:icon="@drawable/icon" android:debuggable="true">
Thus if we register using the
[Application]
attribute, we then will see our custom application class used instead:<application android:label="App6" android:name="md50033386ba710bcf156abf7e9c48d30ef.MyApplication" android:allowBackup="true" android:icon="@drawable/icon" android:debuggable="true">
Here's a complete working example of this:
Without a name defined in the comment above, you'll get a md5 hashed named instead.
There's a few notes in our Architecture docs that might be beneficial here as well:
https://developer.xamarin.com/guides/android/under_the_hood/architecture/#Application_Startup
It may be the debugger problem. In onCreate(), try doing something that you can see in your activity, for example setting text in TextView.
In Java you'd have to set the
android:name
attribute of theapplication
tag in theAndroidManifest.xml
file to.MyApplication
. Presume it's similar in Xaramin: