LoadingDialogUtils数据加载框

前言:直接上使用语句

引用

 LoadingDialogUtils.show(AppManageActivity2.this);//展示
 LoadingDialogUtils.dimiss();//隐藏
 LoadingDialogUtils.unInit();//注销加载框,避免内存泄露。

核心类如下:

1、单例模式;
2、含有上下文的弱引用,避免内存溢出;

import android.app.Activity;
import android.app.ProgressDialog;

import java.lang.ref.WeakReference;

/**
 * Created by 方毅超 on 2017/7/19.
 * 数据加载框
 */
public class LoadingDialogUtils {
    /**
     * 数据访问等待框
     */
    private static ProgressDialog loadingDialog;
    private static WeakReference reference;

    public static void init(Activity act) {
        if (loadingDialog == null || reference == null || reference.get() == null || reference.get().isFinishing()) {
            reference = new WeakReference<>(act);

            loadingDialog = new ProgressDialog(reference.get());
            loadingDialog.setMessage("加载中...");
            loadingDialog.setCancelable(false);
        }
    }

    public static void setCancelable(boolean b) {
        if (loadingDialog == null) return;
        loadingDialog.setCancelable(b);
    }

    /**
     * 显示等待框
     */
    public static void show(Activity act) {
        init(act);
        loadingDialog.show();
    }

    /**
     * 隐藏等待框
     */
    public static void dismiss() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }


    /**
     * 注销加载框,避免发生内存泄露
     */
    public static void unInit() {
        dismiss();
        loadingDialog = null;
        reference = null;
    }
}

你可能感兴趣的:(LoadingDialogUtils数据加载框)