Reputation: 25
I am looking to implement a generic type API Handler. I have created a class called ApiCall that extends the AsyncTask. The results are returned as a JSONArray.
I am new to Android and don't fully understand how to invoke a function in the Activity Class. I have seen several posts and blogs in regards to how to do this; however, most of them are either a private class withing the activity class or have some type of call back to the activity or fragment that provides a hard link back to the calling class.
I want to keep this class loosely coupled, to allow me to reuse this throughout the app.
Thank you for your insight.
Upvotes: 1
Views: 45
Reputation: 1453
You can pass a reference as a Weak reference. This will ensure that the class is loosely coupled to any UI elements.
private WeakReference<CallBack> callBack;
//To assign a callBack reference to weak reference object
callBack = new WeakReference<CallBack>(callBackReference);
//To check if reference to callBack still available
final CallBack callback = callBack.get();
if(callback!= null){
}
Upvotes: 1