5

I have a class which extends Application and i want to call it from code, it have

@Override
public void onCreate() 

I need to call this from an Activity. I know how to call it when app starts for that i need to include in manifest:

 android:name=""

Thanks.

Streetboy
  • 4,351
  • 12
  • 56
  • 101

5 Answers5

13

You should avoid Calling Applications onCreate manually, as it will get started automatically if anything is configured correctly. However, If you want to call Methods from your overridden Application you can do it like this:

public class MyApplication extends Application{
    public void someMethod(){}
}

then inside any Activity:

MyApplication app = (MyApplication)getApplication()
app.someMethod();
Rafael T
  • 15,401
  • 15
  • 83
  • 144
  • I get this exception java.lang.ClassCastException: android.app.Application – Streetboy Apr 26 '12 at 12:44
  • then you don't registered your Application inside your Manifest – Rafael T Apr 26 '12 at 13:20
  • then you can try to declare your Library Application.class inside your Projetcs Manifest. Not shure this will work, but at least, it works for Activitys. Also have a look at this: http://stackoverflow.com/questions/9499044/writing-an-android-library-using-its-own-custom-application-class-getapplicat – Rafael T Apr 26 '12 at 14:20
  • You mean that this class MyApplication which is in is Library project declare in my project which use that library manifest like this android:name="MyApplication" is yes, than this don't work :) – Streetboy Apr 27 '12 at 06:15
  • @Streetboy You should use the fully qualified Name of the Application, not just the class Name – Rafael T Sep 26 '18 at 09:55
3

Try this :

public class YourApplication extends Application 
{     
     public void sayHello {
        System.out.println("Hello")
    }
}

Then call it in any activity by:

YourApplication appState = ((YourApplication)this.getApplication());
appState.sayHello();
Mehdi
  • 1,494
  • 5
  • 33
  • 53
2

Application class onCreate() gets called when the Application starts. If you want to call a method that you have declared in your Application class you can call it like,

((Application_Class_Name)getApplicationContext()).calling_method();

From any other class that extends Activity, else you have to use context to get the instance of getApplicationContext() to call from Non Activity class.

Eg - If you want to call it from Adapter class you need to pass the context of the Activity to adapter class and get the instance of Application,

((Application_Class_Name)mContext.getApplicationContext()).calling_method();
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
0

From Activity you simply call ((YourAppName)getApplicationContext()).

And also, you don't need onCreate() in your Application (unless you know you do). You can set some methods there and then call them with e.g. ((YourAppName)getApplicationContext()).myMethod(). Your app is alive as long as any of its activities is.

Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
  • are you shure that `getApplicationContext()` returns the same than `getApplication` ? – Rafael T Apr 26 '12 at 12:31
  • 1
    To be honest I never heard of getApplication(). I use getApplicationContext() in my app, and it works like a charm. Btw, lots of answers showed up;) – Michał Klimczak Apr 26 '12 at 12:32
0

You should't call onCreate() method by yourself... Android does it for you... The main purpose of such a class is to keep global variable commom to whole application, since Application itself is a single instance...

And it lets you override onCreate() because you may need your Custom things in an Appliucation/ACtivity that the OS does create..

ngesh
  • 13,398
  • 4
  • 44
  • 60