java 自定义 android.app.Application 不触发 onCreate 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6858158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:37:11  来源:igfitidea点击:

custom android.app.Application not firing onCreate event

javaandroidevents

提问by Narcís Calvet

I'm deriving a custom application from android.app.Application and I can't get its onCreate event being fired. Here's the implementation

我正在从 android.app.Application 派生一个自定义应用程序,但我无法触发它的 onCreate 事件。这是实现

import android.app.Application;

public class MyApplication extends Application {

    public MyApplication() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
}

And here's how I'm using it:

这是我如何使用它:

MyApplication ctrl = new MyApplication();

回答by Balaji Khadake

Add following in your AndroidManifest.xml

在您的 AndroidManifest.xml

<application
    android:name="MyApplication"
    android:debuggable="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name">
</application>

then your onCreate()will get fired.

那么你的onCreate()遗嘱就会被解雇。

回答by Brad

I had this issue and found that in my case that the whole issue was phone side. I rebooted the phone and that fixed the issue.

我遇到了这个问题,并发现在我的情况下,整个问题都在电话方面。我重新启动了手机并解决了问题。

回答by Barakuda

Very simple

很简单的

In your AndroidManifest.xml, within the applicationtag enter the name of your Applicationsub-class with it's path under the android:nameattribute.

在您的AndroidManifest.xml, 在application标签内输入您的Application子类的名称及其android:name属性下的路径。

Example:

例子:

<application
...
android:name=".models.custom.BaseApplication"
...
> ... </application>

回答by pawelzieba

Don't construct it, get it from Context.

不要构建它,从Context.

For example from Activity:

例如来自Activity

MyApplication ctrl = (MyApplication)getApplicationContext();

More info: Context.getApplicationContext()

更多信息: Context.getApplicationContext()

Documentation says that onCreate()is

文档说onCreate()

Called when the application is starting, before any other application objects have been created

在应用程序启动时调用,在创建任何其他应用程序对象之前

回答by keyboardsurfer

You don't actually create instances of your Activities with the newoperator. Instead you start an Intent like this:

您实际上并没有使用new操作员创建您的活动的实例。相反,您可以像这样启动 Intent:

Intent start = new Intent(context, Classname.class);
context.startActivity(start);

When creating an object with the newoperator, then onCreatenever will be called.

当使用new操作符创建对象时,onCreate永远不会被调用。

[EDIT] When creating Applications with the newoperator onCreatewon't be called either[/EDIT]

[编辑] 使用new运算符创建应用程序时onCreate也不会被调用[/编辑]

[EDIT2] You could create a static method that returns the application like this:

[EDIT2] 您可以创建一个静态方法来返回这样的应用程序:

public static MyApplication getApp() {
    return mInstance;
}

[/EDIT2]

[/编辑2]

回答by Naveen Kumar

As Balaji Mentioned if your still facing issue even after mentioning class name under application tag

正如 Balaji 所提到的,如果您在应用程序标签下提到类名后仍然面临问题

<application
    android:name="MyApplication"
    android:debuggable="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"> </application>

Then try this:

然后试试这个:

Try and disabling Instant Runand then clean project and Rebuild it and then run again. It worked for me. Thanks.

尝试禁用 Instant Run,然后清理项目并重建它,然后再次运行。它对我有用。谢谢。