custom android.app.Application not firing onCreate event

21,055

Solution 1

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.

Solution 2

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.

Solution 3

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>

Solution 4

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]

Solution 5

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

Share:
21,055
Narcís Calvet
Author by

Narcís Calvet

I'm a software developer and parent. In my free time I like to: ride on MTB, watch F.C. Barcelona (soccer) games and listen to prog music.

Updated on May 29, 2021

Comments

  • Narcís Calvet
    Narcís Calvet almost 3 years

    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.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();
    
  • Narcís Calvet
    Narcís Calvet almost 13 years
    It doesn't work. I have the main Activity defined as an application by the fault at AndroidManifest.xml and this additional application tag. Do you think this is correct?
  • Selvin
    Selvin almost 13 years
    it's not Activity it's Application
  • Ben Weiss
    Ben Weiss almost 13 years
    oops, my bad. But that also applies to an Application. If you create your application with the new operator, onCreate won't be called.
  • Narcís Calvet
    Narcís Calvet almost 13 years
    Yes, I did that but I need to be able to get object instance to be able to set object properties and call its methods.
  • Narcís Calvet
    Narcís Calvet almost 13 years
    Doesn't work for me either. Actually the application crashes at this line.
  • Narcís Calvet
    Narcís Calvet almost 13 years
    The line you suggested: MyApplication ctrl = (MyApplication)getApplicationContext();
  • pawelzieba
    pawelzieba almost 13 years
  • Ben Weiss
    Ben Weiss almost 13 years
    you could create a static method that returns the singleton of this Application.
  • Narcís Calvet
    Narcís Calvet almost 13 years
    Thanks. The problem here is that I should also declare that application at AndroidManifest.xml but, according to Balaji's reply and comments, I can't have 2 applications defined there. Can you think of a solution to this?
  • Dr.jacky
    Dr.jacky almost 9 years
    @BalajiKhadake My app class extends from a library application class and onCreate in library doesn't fired!
  • Jozka Jozin
    Jozka Jozin over 8 years
    Must add class name to android:name, like android:name="your.package.com.MyApplication".
  • Dr. aNdRO
    Dr. aNdRO over 6 years
    You cannot return this from a static method -_-
  • Mickael Bergeron Néron
    Mickael Bergeron Néron almost 3 years
    Dude. You potentially saved me hours with this answer. Thanks.
  • Shubham Mogarkar
    Shubham Mogarkar about 2 years
    it is true that it is phone specific problem, but restarting the device did not solve the problem in my case.