简体   繁体   English

从清单扩展适用于OnCreate不匹配的Android应用程序类

[英]Extending Android Application class for OnCreate mismatch from manifest

Android application using Xamarin but I think this applies to non-Xamarin. Android应用程序使用Xamarin,但我认为这适用于非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. 在Android应用程序中,您会注意到AndroidManifest.xml文件包含一个条目,用于配置应用程序的各种属性:图标,标签,主题等。这足以满足您的应用程序需求。 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. 实例化后,该应用程序将基于清单中配置的值执行必要的初始化,然后它将加载定义为视图的Activity以在启动时加载。

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). 在某些情况下,您可能希望扩展Application类,以便在应用程序启动时执行自定义初始化,或维护全局应用程序状态(例如,在游戏中跟踪多个级别的得分的情况下)。 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. 上面显示的示例是某人决定对Application类进行子类化并覆盖它的OnCreate方法的情况,尽管他们没有在override方法中包括任何自定义代码。 It looks like their purpose was to create a global static property to always have access to the Activity that is currently loaded. 看起来他们的目的是创建一个全局静态属性,以始终有权访问当前加载的Activity。

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. 但是,最近我需要扩展应用程序类,以便可以为应用程序使用OnCreate事件。

I added a new class which extends Application and it looks like this: 我添加了一个扩展Application的新类,它看起来像这样:

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! 谢谢!

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. Android Manifest的主题以及其他所有内容都将保留在那里,Android Application类和AndroidManifest.xml可独立工作,因此添加扩展的应用程序类不需要对Manifest文件进行任何更改,反之亦然。

Good luck, 祝好运,

Feel free to revert in case of queries 如有疑问,请随时还原

UPDATE UPDATE

See to it that your application class has the [Application] attribute on top of the class name 确保您的应用程序类在类名的顶部具有[Application]属性

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

public class MyApp: Application
 {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM