How To Call Method In Activity Form Non Activity Class
I have an Activity and non Activity class. How to call a method in Activity class from non Activity class public class MainActivity extends Activity { public void onCreate(Bund
Solution 1:
Just create a callback interface inside the DateClass.
publicDateClass {
publicinterface IDateCallback {
voidcall(ArrayList<String> arr);
}
private IDateCallback callerActivity;
publicDateClass(Activity activity) {
callerActivity = (IDateCallback)activity;
}
...
}
publicvoidshow(ArrayList<String> array) {
callerActivity.Call(array);
...
}
//And implements it inside your activity.publicclassMainActivityextendsActivityimplements IDateCallback {
publicvoidcall(ArrayList<String> arr) {
}
}
Solution 2:
Well there are several things you could do. I think the easiest for you would be to send the Context
into DataClass
like so:
DataClassdc=newDataClass();
dc.show(this);
And in your DataClass
save the context into a global var Context context
. Then use it like so:
((MainActivity)context).call(array);
Solution 3:
((MainActivity)getContext).array();
Solution 4:
Just make a singleton like:
TeacherDashboardSingleton:
publicclassTeacherDashboardSingleton {
public Teacher_Dashboard aa;
privatestaticfinalTeacherDashboardSingletonourInstance=newTeacherDashboardSingleton();
publicstatic TeacherDashboardSingleton getInstance() {
return ourInstance;
}
}
myActivity class:
onCreate(....){
....
TeacherDashboardSingleton.getInstance().aa = this;
....
}
this will create an object of same instance as in activity
now you can use it from anywhere
Post a Comment for "How To Call Method In Activity Form Non Activity Class"