custom android.app.Application not firing onCreate event

前端 未结 6 562
粉色の甜心
粉色の甜心 2021-01-01 09:31

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

import android.ap         


        
相关标签:
6条回答
  • 2021-01-01 09:57

    Don't construct it, get it from Context.

    For example from Activity:

    MyApplication ctrl = (MyApplication)getApplicationContext();
    

    More info: Context.getApplicationContext()

    Documentation says that onCreate() is

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

    0 讨论(0)
  • 2021-01-01 10:00

    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.

    0 讨论(0)
  • 2021-01-01 10:03

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

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

    When creating an object with the new operator, then onCreate never will be called.

    [EDIT] When creating Applications with the new operator onCreate won't be called either[/EDIT]

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

    public static MyApplication getApp() {
        return mInstance;
    }
    

    [/EDIT2]

    0 讨论(0)
  • 2021-01-01 10:14

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

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

    Then try this:

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

    0 讨论(0)
  • 2021-01-01 10:16

    Very simple

    In your AndroidManifest.xml, within the application tag enter the name of your Application sub-class with it's path under the android:name attribute.

    Example:

    <application
    ...
    android:name=".models.custom.BaseApplication"
    ...
    > ... </application>
    
    0 讨论(0)
  • 2021-01-01 10:17

    Add following in your 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.

    0 讨论(0)
提交回复
热议问题