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.
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();
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();
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();
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.
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..
Related
What must I do in order to ensure that the app is always initialised, even if the user or OS kills it?
public static void init(){/*some init and checks here*/}
if an MyAlarmReceiver.onReceive() or MyBroadcastReceiver.onReceive() is called by the OS... how to automatically call init() without putting it as the first statement in the MyAlarmReceiver.onReceive(), MyBroadcastReceiver.onReceive(), etc?
Create a class that extends Application and put the statement in the onCreate method
Ref: https://developer.android.com/reference/android/app/Application.html#onCreate()
You can add a static block in your class, this code block will be executed when this class this loaded by ClassLoader, that means any usage of this class will cause the ClassLoader load this class into memory
public class Test extends BroadcastReceiver{
static{
//do something
}
}
Unable to access the methods from one class to another class in android.
Actually, what I am trying is
ClassA.java
class ClassA extends Activity
{
method_1();
}
ClassB.java
class ClassB extends BroadCastReciever
{
// I need to access the method_1 from the class ClassA.
}
How can I do this?
First Solution
Declare your method1() as follows -
public static void method_1()
{
}
Then you can access it from ClassB as follows -
ClassA.method1();
Second Solution
Create an object of ClassA and access method1() from ClassB as follows-
ClassA classA = new ClassA();
classA.method1();
EDIT:
If both the classes are in the same package then you can use protected instead of public in the First Solution.
inside ClassA declare public method from ClassB create object of ClassA and access method using object
write below code inside ClassB
Like
ClassA classa = new ClassA();
classa.method_1();
Note :
if possible don't create class as static if it is not necessary.
provide class modifier to public.
mark method_1(); as public and static
Eg:
class ClassA extends Activity
{
public void static method_1();
}
Then you should be able to access method_1(); without creating a object of class A.
i guess you cant access a method from a class that extends an activity in android , however you can add an inner or helper class to the activity class like this example :
Call a public method in the Activity class from another class?
there is two way to access it
1) you need to create method as public static method() so that you can access it anywhere by calling class name dot method name like Aaa.method();
2)or you need a object to call method like passing Aaa's object to broadcast class and call method on it.
EDIT:
if you are passing context from activity A to brodcast class then you can convert context into activity object to call its public methods.
like:
in brodcast class:
((yourActivityName)context).method();
Basically you can call any method from another class, as long as it's public. If the method is static, you don't even need to instantiate it. But read some tutorials to get the basic idea of OOP instead of just looking for a solution for your specific problem, it'll help a lot more!
You should never be able to to that ... How do you know if the activity is still alive? The best way to send messages from BroadcastReceivers to Activities (where you register a custom receiver in onCreate/onresume and unregister in onDestroy/onPause) is to send another broadcast message from BroadcastReceiver.
But you could catch the initial broadcast already in activity. Consider using OrderedBroadcastReceivers.
So I'm trying to make an application using the Singleton Method. I want to have a class that stores all the information about my device's bluetooth state/connections/devices, and I want to make multiple activites that can access these methods.
I know that I need to have a class that extends Application, then I can access everything by calling getApplication(). What I do not understand, is where I initialize this object. From my frame of reference, I have all of these separate Activities, and if I initialize the object in one, I'm going to need to use intents to pass the object to the next activity, which completely defeats the purpose of using the singleton method.
Any help would be appreciated, thanks.
Simply extend from android.app.Application. Then register it as the application class in your AndroidManifest.xml:
<application android:name="mypackage.MyApplication" ...>
In your class you will receive usual Android calls, such as in
#Override
public void onCreate() { }
where you will able to initialize your global instances.
In the activities fetch the instance of MyApplication downcasting with:
MyApplication app = (MyApplication) getApplication();
Hope that helps.
If you extended Application your class will be created as your app launches. It can be retrieved in Activity classes using getApplication()
Check here : http://www.kodejava.org/examples/12.html
and here : http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/
and here : http://inchoo.net/mobile-development/android-development/android-global-variables/
We all know you can't use static on findViewById so ...
The basic code is:
public class DiffViewFlowExample extends Activity implements OnItemClickListener {
#Override
public void onCreate() {
}
static void hereismymethod() {
}
How can I use a findViewById here? I know I can't locally but because static won't work then...
Oh you might say: add it as an argument,well I would,but I will call the hereismymethod from a service,and we all know that services don't like to play with stuff that is about display...
So can anybody save me?
You shouldn't be calling method2, static or not, on an Activity from outside of the Activity. If you want to act upon your Activity from within the Service, then create a listener interface and then register your activity as a listener inside of the service. Then, when it's appropriate, have your service find the view and act on it. To allow the service to access the views in your Activity, simply make your interface method pass a reference to the Activity. Then, the Service can call activity.findViewById() and do whatever it wants to it.
I have two different class(class A and Class B). i want to use the method of Class A in Class B. i normally used object for class A and called method in class B. but unfortunately i am getting Force close error. Is that any thing different to call a method of another class in android. I referred many articles in stackoverflow. but i cant understand properly. pls help me to find out the solution.
You should not create object like this, you should use context to call object like as below
((Class A) contextObject).function();
it runs perfectly on my system,
earlier class A and class B both are extending Activity and now only A extends Activity and B extends A and now B can call functions of A
this works for me:-
public class A extends Activity
{
functionOfA(){}
}
public class B extends A
{
//calling function of class A
functionOfA();
}
In case of Android, class which extends Activity will maintain its life cycle methods. if method which is defined in different class other current running activity may be killed or in pause state. so it is suggested that if method which is reusable in application should in different class for example (AppManager singleton class) rather than being in single activity class
u have to create constructor of class A
& in class B make an obj to class A
initialize it with
ClassA obj=new classA();
obj.method_A();
hope this will help
As I said in my comment, you shouldnt instantiate activities.
If your method uses some method that are called from a Context object, you can create a new class ( Class NewClass) that accepts a context parameter and implements your methods in it.
So this way, you can call your class from any activity:
NewClass nc = new NewClass(this);
Look up for some example of how to use a database in Android. It uses the same way.