Skip to content Skip to sidebar Skip to footer

How To Perform Some Actions Before Letting User To Do Anything?

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 MyAp

Solution 1:

The key item here is to "Register" your custom Application class by using the [Android.App.Application] attribute. Secondly you need to provide an OnCreate 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 the AndroidManifest.xml. Rather we use the default android.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:

[Application] //Name is typically a good idea as wellpublicclassMyApplication : Application
{
    publicMyApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    { }
    publicoverridevoidOnCreate()
    {
            base.OnCreate();
            int myInt = 1;
    }
}

enter image description here

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

Solution 2:

In Java you'd have to set the android:name attribute of the application tag in the AndroidManifest.xml file to .MyApplication. Presume it's similar in Xaramin:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name=".MyApplication"
    >

Solution 3:

The Xamarin.Android way of add your own Android Application class:

1) Specific the class Name:

[Application(Name = "com.sushihangover.MyAndroidAppClass")]
publicclassMyApplication : Application
{
    publicMyApplication(IntPtr handle, JniHandleOwnership transfer) : base(handle,transfer) { }
    publicoverridevoidOnCreate()
    {
        base.OnCreate();
        Log.Debug("SO", "App OnCreate");
    }
}

2) Update your manifest:

Add this fully qualified name as a android:name= attribute:

~~~
<applicationandroid:name="com.sushihangover.MyAndroidAppClass"android:allowBackup="true"android:icon="@mipmap/icon"android:label="@string/app_name"></application>
~~~

Solution 4:

It may be the debugger problem. In onCreate(), try doing something that you can see in your activity, for example setting text in TextView.

Post a Comment for "How To Perform Some Actions Before Letting User To Do Anything?"