Michael Bedford
Michael Bedford

Reputation: 1824

Extending Android Application class for OnCreate mismatch from manifest

Android application using Xamarin but I think this applies to non-Xamarin. I read this article while doing research.

In an Android application, you will notice that the AndroidManifest.xml file contains an entry for which is used to configure various properties of your application: icon, label, theme, etc. Depending on your application's needs, this is sufficient. The application when instantiated will perform the necessary initializations based on the values configured in the manifest, and it will then load the Activity that is defined as the view to load on startup.

In some cases, you may wish to extend the Application class in order to perform custom initialization on application startup, or to maintain global application state (for example, in the case of a game that is tracking score across multiple levels). The example that you have shown above is the case where someone has decided to subclass the Application class and override it's OnCreate method, although they haven't included any custom code in the override method. It looks like their purpose was to create a global static property to always have access to the Activity that is currently loaded.

Previously, I did not need to extend the application class so I just had my manifest handle it like this article says. However, I recently had a need come up to extend the application class so I can use the OnCreate event for the application.

I added a new class which extends Application and it looks like this:

namespace MyApp.Droid
{
     public class MyApp : Application
     {
        public MyApp(IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer)
        :base(handle, transfer)
        {

        }

        public override void OnCreate()
        {
            base.OnCreate();

            //Do OnCreate things here.
        }
    }
}

It seems just adding this class is not complete. First question, do I also have to modify my manifest now? Second question, the class can have attributes for theme, icon, etc... which are and have been defined in my manifest. Can they stay in the manifest or do I need to move them to this new class as attributes?

Here is the manifest:

    <application android:allowBackup="true" android:icon="@drawable/mainapplogo" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">

Thank you!

Upvotes: 1

Views: 262

Answers (1)

FreakyAli
FreakyAli

Reputation: 16572

First question, do I also have to modify my manifest now?

In this question if you mean just because you have added the new application class is there a need to make any change in the existing manifest then NO there is no such requirement.

Second question, the class can have attributes for the theme, icon, etc... which are and have been defined in my manifest. Can they stay in the manifest or do I need to move them to this new class as attributes?

The Android Manifest's Themes and everything else will stay there, The Android Application class and AndroidManifest.xml work independently and hence adding an extended application class does not require any change in your Manifest file or vice versa.

Good luck,

Feel free to revert in case of queries

UPDATE

See to it that your application class has the [Application] attribute on top of the class name

#if DEBUG
[Application(Debuggable=true)]
#else
[Application(Debuggable = false)]
#endif

public class MyApp: Application
 {

Upvotes: 2

Related Questions