9

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(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        DataClass dc = new DataClass();
        dc.show();                  
    }

    public void call(ArrayList<String> arr) {
       // Some code...
    }
}

public class DataClass {

    public void show(ArrayList<String> array) {
        // Here I want to send this ArrayList values into the call
        // method in activity class.

       MainActivity act = new MainActivity();
       act.call(array);                  
    }
}
SpArk
  • 5
  • 3
user2932841
  • 351
  • 2
  • 4
  • 4
  • See also http://stackoverflow.com/questions/19666572/how-to-call-a-method-in-another-activity-from-activity and http://stackoverflow.com/questions/16653867/android-call-method-from-an-other-activity – RenniePet Dec 27 '15 at 08:27

4 Answers4

14

Just create a callback interface inside the DateClass.

public DateClass {
    public interface IDateCallback {
        void call(ArrayList<String> arr);
    }  

    private IDateCallback callerActivity;

    public DateClass(Activity activity) {  
        callerActivity = (IDateCallback)activity;  
    }
...  
}  

public void show(ArrayList<String> array) {            
    callerActivity.Call(array);  
    ...  
}

//And implements it inside your activity.

public class MainActivity extends Activity 
        implements IDateCallback {  

    public void call(ArrayList<String> arr) {

    }  
}
roob
  • 1,116
  • 7
  • 11
kameny
  • 2,372
  • 4
  • 30
  • 40
  • Yes I think this should be the preferred way to do it. But a question regarding this approach, since we're passing an activity reference to the DateClass, won't it result in a memory leak since the activity won't be able for garbage collection because the DateClass holds a reference to it? – akshayt23 Feb 18 '16 at 13:44
  • Good question, to be honest I haven't got a clear answer to that question. I think it is not a problem in that case, because the activity is active and as I understand the "inner" Date class is doing something in the Activity. My assumption is: when the Activity is going to be finished, both of the classes are going to be garbaged. – kameny Feb 18 '16 at 14:30
7

Well there are several things you could do. I think the easiest for you would be to send the Context into DataClass like so:

DataClass dc =new DataClass();
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);
Andy
  • 10,553
  • 21
  • 75
  • 125
2
((MainActivity)getContext).array();
0

Just make a singleton like:

TeacherDashboardSingleton:

public class TeacherDashboardSingleton {
public  Teacher_Dashboard aa;
private static final TeacherDashboardSingleton ourInstance = new TeacherDashboardSingleton();
   public static 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