37

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
  • 7,304
  • 5
  • 27
  • 47
  • 1
    `ctrl.onCreate();` will do it for you ... – Selvin Jul 28 '11 at 11:33
  • Yes, it does. But shouldn't this be fired automatically? I can call onCreate() in the constructor as well but I don't think this is the most elegant solution. – Narcís Calvet Jul 28 '11 at 11:48
  • 1
    you should call MyApplication constructor at all... you should point this class in manifest xml and Android OS should call it ... and onCreate too – Selvin Jul 28 '11 at 11:50
  • And how should I declare an Application called from an Activity in the AndroidManifest? By default an application tag is already added there. – Narcís Calvet Jul 28 '11 at 12:00
  • why you need to extends standard Application class ... – Selvin Jul 28 '11 at 12:08
  • To be able to create and object from which I can call methods and modify properties. Using Intent to create such object instances didn't allow me to do that. – Narcís Calvet Jul 28 '11 at 12:10
  • for global variables ? ... not a good idea ... what you wana achive with this ? – Selvin Jul 28 '11 at 12:16
  • No, not global variables, object properties and methods. My optimal solution would be what I posted at http://stackoverflow.com/questions/6792331/sluggish-zoom-and-scroll-with-gridview-in-android. However, due to this issue I can't follow this approach until I find a satisfactory solution. – Narcís Calvet Jul 28 '11 at 13:22

8 Answers8

95

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.

Seraphim's
  • 12,559
  • 20
  • 88
  • 129
Balaji Khadake
  • 3,449
  • 4
  • 24
  • 38
  • 1
    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? – Narcís Calvet Jul 28 '11 at 12:08
  • 1
    @BalajiKhadake My app class extends from a library application class and onCreate in library doesn't fired! – Dr.jacky May 14 '15 at 06:48
  • 4
    Must add class name to android:name, like android:name="your.package.com.MyApplication". – Jozka Jozin Aug 17 '15 at 15:43
11

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.

Brad
  • 280
  • 4
  • 7
4

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>
Barakuda
  • 790
  • 8
  • 16
2

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]

Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
1

In Manifest file Make sure u have added the tag i.e class which extends Application.

Tag= android:name=".MyApplication

ManifestFile

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.services">
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Services">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--Declaring the Service in the manifest file-->
        <service
            android:name=".MyServiceDemo"
            android:foregroundServiceType="dataSync" />
    </application>
</manifest>

MyApplication.java(i.e class which extends Applicatoin)

package com.example.services;

import android.app.Application;

public class MyApplication extends Application {

    private static MyAppsNotificationManager notificationManager;

    @Override
    public void onCreate() {
        super.onCreate();
        notificationManager = MyAppsNotificationManager.getInstance(this);
        notificationManager.registerNotificationChannelChannel(
                getString(R.string.channelId),
                "BackgroundService",
                "BackgroundService");
    }

    public static MyAppsNotificationManager getMyAppsNotificationManager(){
        return notificationManager;
    }
}
vinay shetty
  • 895
  • 1
  • 9
  • 15
1

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

pawelzieba
  • 16,082
  • 3
  • 46
  • 72
0

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.

Naveen Kumar
  • 979
  • 1
  • 13
  • 24
0

My problem was that I needed to log something and it wasn't called Application onCreate().

If you have the same problem and need some Side Effects to trigger like Analytics events. Use these 2 approaches.

Don't put your main logic (for example don't initialize the libraries) in the below places.

Approach 1: Override onCreate of your Application Class like below:

     @OptIn(DelicateCoroutinesApi::class)
     override fun onCreate() {
        GlobalScope.launch {
          ProcessLifecycleOwner.get().lifecycle.withCreated {
            // Here onCreate of the Application will be called !
          }
        }
        super.onCreate()
      }

Approach 2: Create a class for example named AppLifecycleListener that implements DefaultLifecycleObserver and override required methods.

@Singleton
class AppLifecycleListener @Inject constructor() : DefaultLifecycleObserver {

override fun onCreate(owner: LifecycleOwner) {
    super.onCreate(owner)
    // Here onCreate of the Application will be called !
   }
}

Don't forget to addObserver() your class in Application

@Inject
lateinit var appLifecycleListener: AppLifecycleListener

override fun onCreate() {
    super.onCreate()
    ProcessLifecycleOwner.get().lifecycle.addObserver(appLifecycleListener)
}
Sepehr
  • 960
  • 11
  • 17