Call A Public Method In The Activity Class From Another Class?
Solution 1:
Why not just pass the activity to the constructor like
publicclassMyActivityextendsActivity {
onCreate(){
MyClass myobj=new MyClass(MyActivity.this);
}
publicvoidmyMethod(){
}
}
//HELPER CLASS IN A SEPARATE FILE publicclassMyClass{
publicMyClass(MyActivity act) {
act.myMethod();
}
}
Solution 2:
Make that method as static so you can call without creating the class object
publicstaticvoidMymethod()
{}
and call like this way
MainActivity.Mymethod();
Solution 3:
This is probably the best way to do it. This is how I'm doing it. It's called a Singleton Design Pattern:
publicclassMyActivityextendsActivity {
privatestaticMainActivity instance;
publicstaticMainActivitygetInstance() {
if(instance==null){
setInstance(this);
}
return instance;
}
publicstaticvoidsetInstance(MainActivity instance) {
MainActivity.instance = instance;
}
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setInstance(this);
}
}
Solution 4:
If I'm understanding you correctly I believe you can solve your problems using an interface as a callback.
////ACTIVITY/////////////////////////////////
publicclassMyActivity() extendsActivity {
onCreate()
{
MyClass myObj=newMyClass();
//Set the listener on the object. Created as anonymous
myObj.setListener(newMyClass.Listener() {
myMethod();
});
}
}
publicvoidmyMethod(){
}
//////Custom Class//////////////////
publicclassMyClass {
Listener mListener;
publicinterfaceListener {
publicvoidonInterestingEvent();
}
publicvoidsetListener(Listener listener) {
mListener = listener;
}
publicvoidsomeUsefulThingTheClassDoes() {
//Do your code here and when you're ready to call the activity's method do this
mListener.onInterestingEvent();
}
}
Solution 5:
I had an inner class that I wanted to pull out into a more general library "Helper" class. I had the same issue you do. I got around it by making the helper class abstract, with a single abstract method. Then in my project package I extended the helper class with a constructor call in the specific class.
publicclassMyActivityextendsActivity {
onCreate() {
MyHelperClass = new MyHelperClass(this, "foobar");
}
publicvoidmyMethod() {
// Code...
}
}
// In a different filepublicclassMyHelperClassextendsHelperClass {
private MyActivity mInstance;
publicMyHelperClass(MyActivity act, String data) {
super();
this.mInstance = act;
this.mActivity = act; // Useful for calling generic Activity methods in the HelperClassthis.mData = data;
}
protectedvoidcallMyActivityMethod() {
mInstance.myMethod();
}
}
// In a different filepublicabstractclassHelperClass {
protected Activity mActivity;
protected String mData;
publicHelperClass() {
// Subclass will set variables
}
protectedabstractvoidcallMyActivityMethod();
// More code for all the other stuff the class does
}
In this way, I have a helper class that contains the vast majority of the "work", and all I have to do is make a subclass with the constructor and one method in order to get access to the calling activity's method of interest.
Post a Comment for "Call A Public Method In The Activity Class From Another Class?"