Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call class which extends Application android?

Tags:

android

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.

like image 799
Streetboy Avatar asked Dec 05 '22 16:12

Streetboy


2 Answers

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();
like image 176
Rafael T Avatar answered Jan 11 '23 23:01

Rafael T


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();
like image 20
M3HD1 Avatar answered Jan 12 '23 01:01

M3HD1