LeakCanary使用方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/neabea2016/article/details/84934866

1.gradle导入包

 debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.2'
 releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'
    // Optional, if you use support library fragments:
 debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.2'

2.Application getRefWatcher()方法是为了检测fragment,activity不需要单独配置

public class AppApplication extends Application { 
    private RefWatcher refWatcher;
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // 判断是否和 LeakCanary 初始化同一进程
            return;
        }
        refWatcher = LeakCanary.install(this);
    }

    public static RefWatcher getRefWatcher(Context context) {
        if (context == null) {
            return null;
        }
        AppApplication application = (AppApplication) context.getApplicationContext();
        return application.refWatcher;
    }
}

3.BaseFragment

    @Override public void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = MyApplication.getRefWatcher(getActivity());
        if (refWatcher == null) return;
        refWatcher.watch(this);
    }

猜你喜欢

转载自blog.csdn.net/neabea2016/article/details/84934866