I’m trying to reduce the noise in my activity classes (Android) and the first thing I’ve pulled out is the async tasks I had as inner classes. The only issue I’m having is that I’m not able to pass the AppDelegate ref from the activity to the async task once it’s moved out.
Here is my (failed) attempt at it
//here I pass the ref from the activity
public class HelloAndroidActivity extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
GetSessionsAsyncTask task = new GetSessionsAsyncTask(HelloAndroidActivity.this, getApplicationContext());
task.execute();
}
}
//here I push it to a field to use it later (but it fails during the init of this obj)
public class GetSessionsAsyncTask extends AsyncTask<String, Void, List<Session>> {
private Activity activity;
private AppDelegate delegate;
public GetSessionsAsyncTask(Activity activity, Context context) {
this.activity = activity;
this.delegate = (AppDelegate) context;
}
}
Edit
the app delegate I mention above just extends application (android class)
public class AppDelegate extends Application {
}
You need to declare AppDelegate to be your Application class in the Manifest: