custom android.app.Application not firing onCreate event
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 new
operator. 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
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, 2021Comments
-
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 almost 13 yearsIt 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 almost 13 yearsit's not Activity it's Application
-
Ben Weiss almost 13 yearsoops, 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 almost 13 yearsYes, 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 almost 13 yearsDoesn't work for me either. Actually the application crashes at this line.
-
Narcís Calvet almost 13 yearsThe line you suggested: MyApplication ctrl = (MyApplication)getApplicationContext();
-
pawelzieba almost 13 years
-
Ben Weiss almost 13 yearsyou could create a static method that returns the singleton of this Application.
-
Narcís Calvet almost 13 yearsThanks. 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 almost 9 years@BalajiKhadake My app class extends from a library application class and onCreate in library doesn't fired!
-
Jozka Jozin over 8 yearsMust add class name to android:name, like android:name="your.package.com.MyApplication".
-
Dr. aNdRO over 6 yearsYou cannot return this from a static method -_-
-
Mickael Bergeron Néron almost 3 yearsDude. You potentially saved me hours with this answer. Thanks.
-
Shubham Mogarkar about 2 yearsit is true that it is phone specific problem, but restarting the device did not solve the problem in my case.