由于弱引用很容易被垃圾收集,因此将一个弱引用的实例分配给范围变量是不是很糟糕,或者每次都应该调用weakReference的get()
方法?
public class someAsyncExample extends AsyncTask<Void, Void, Void> {
private Activity activity;
private Toast toast;
private ProgressDialog progressDialog;
private Activity activity;
public someAsyncExample(WeakReference<Activity> activity) {
this.activity = activity.get();
progressDialog = new ProgressDialog(activity);
}
@override
protected void onPostExecute() {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
toast = new Toast(activity, "Done", TOAST.LENGTH_SHORT);
toast.show();
}
}