Android适当的wearkReference使用

时间:2014-02-17 15:28:47

标签: android android-activity android-lifecycle weak-references

由于弱引用很容易被垃圾收集,因此将一个弱引用的实例分配给范围变量是不是很糟糕,或者每次都应该调用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();
    }

}

0 个答案:

没有答案