下面列出了怎么用com.squareup.leakcanary.RefWatcher的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
//当所有模块集成到宿主 App 时, 在 App 中已经执行了以下代码, 所以不需要再执行
if (BuildConfig.IS_BUILD_MODULE) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, android.support.v4.app.Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
}
@Override
public void onDestroy() {
unregisterUpdateBroadcastReceiver();
threadCancelled = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && networkCallback != null)
try {
((ConnectivityManager) getActivity().getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE)).unregisterNetworkCallback(networkCallback);
} catch (IllegalArgumentException ignored) {
}
if (Build.VERSION.SDK_INT >= 21 && sessionListener != null) {
((MediaSessionManager) getActivity().getSystemService(Context.MEDIA_SESSION_SERVICE))
.removeOnActiveSessionsChangedListener((OnActiveSessionsChangedListener) sessionListener);
}
super.onDestroy();
RefWatcher refWatcher = App.getRefWatcher(getActivity());
refWatcher.watch(this);
}
/**
* Install leak canary for both activities and RIBs.
*/
private void installLeakCanary() {
final RefWatcher refWatcher = LeakCanary
.refWatcher(this)
.watchDelay(2, TimeUnit.SECONDS)
.buildAndInstall();
LeakCanary.install(this);
RibRefWatcher.getInstance().setReferenceWatcher(new RibRefWatcher.ReferenceWatcher() {
@Override
public void watch(Object object) {
refWatcher.watch(object);
}
@Override
public void logBreadcrumb(String eventType, String data, String parent) {
// Ignore for now. Useful for collecting production analytics.
}
});
RibRefWatcher.getInstance().enableLeakCanary();
}
@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
@Override
public void onDestroy() {
RefWatcher refWatcher = App.getRefWatcher(getActivity());
refWatcher.watch(this);
EventBus.getDefault().unregister(this);
super.onDestroy();
}
@Override
public void injectFragmentLifecycle(Context context, List<FragmentManager.FragmentLifecycleCallbacks> lifecycles) {
lifecycles.add(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
((RefWatcher) ArmsUtils
.obtainAppComponentFromContext(f.getActivity())
.extras()
.get(RefWatcher.class.getName()))
.watch(f);
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
// watch for memory leaks
RefWatcher refWatcher = SampleApplication.getRefWatcher(requireActivity());
refWatcher.watch(this);
}
@Override
public void onDestroy() {
((SearchPagerAdapter) ((SearchActivity) getActivity()).getViewPager().getAdapter()).removeFragment(this);
super.onDestroy();
RefWatcher refWatcher = App.getRefWatcher(getActivity());
refWatcher.watch(this);
}
void onDestroy() {
mCheckout = null;
if (mInputMethodResetRequest) performInputMethodServiceReset();
// Watch for the activity to detect possible leaks.
RefWatcher refWatcher = AppHeap.getRefWatcher();
refWatcher.watch(this);
}
public static RefWatcher getRefWatcher(Context context) {
if (context == null) {
return null;
}
BaseApplication application = (BaseApplication) context.getApplicationContext();
if (application.hasMemoryLeak()) {
return application.refWatcher;
}
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
if (BuildConfig.DEBUG) {//Debug的时候检查内存泄露
RefWatcher refWatcher = BaseApplication.getRefWatcher(mContext);
if (refWatcher != null) {
refWatcher.watch(this);
}
}
}
@Override
public void onDestroy() {
stopScanning();
mainMenu = null;
myDevices.onDestroy();
myDevices = null;
super.onDestroy();
// Make sure we don't leak this fragment.
RefWatcher watcher = WhistlePunkApplication.getAppServices(getActivity()).getRefWatcher();
watcher.watch(this);
}
@Override
public void onDestroy() {
super.onDestroy();
RefWatcher refWatcher = LoopApplication.getRefWatcher(getActivity());
refWatcher.watch(this);
}
public static RefWatcher getRefWatcher(Context context) {
UpChainWalletApp application = (UpChainWalletApp) context.getApplicationContext();
return application.refWatcher;
}
public static RefWatcher getRefWatcher(Context context) {
App application = (App) context.getApplicationContext();
return application.refWatcher;
}
@Override
public void onDestroy() {
super.onDestroy();
RefWatcher refWatcher = App.getRefWatcher(mActivity);
refWatcher.watch(this);
}
public static RefWatcher getRefWatcher(Context context) {
App application =(App) context.getApplicationContext();
return application.mRefWatcher;
}
AndroidOFragmentRefWatcher(RefWatcher refWatcher) {
this.refWatcher = refWatcher;
}
SupportFragmentRefWatcher(RefWatcher refWatcher) {
this.refWatcher = refWatcher;
}
@Override
public void onDestroyView() {
super.onDestroyView();
RefWatcher refWatcher = MyApplication.getRefWatcher(getActivity());
refWatcher.watch(this);
}
@Override
public void onCreate(@NonNull Application application) {
if (LeakCanary.isInAnalyzerProcess(application)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
//leakCanary内存泄露检查
ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);
ARouter.openLog(); // 打印日志
ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
ARouter.init(application); // 尽可能早,推荐在Application中初始化
//bugly
CrashReport.initCrashReport(application, "c6dc9a5939", true);
RxToast.init(application)
.setBackgroundColor("#CC000000")
.setTextColor("#FFFFFF")
.setGravity(Gravity.CENTER)
.setPadding(16, 16, 12, 12)
.setMaxLines(2)
.setTextSize(12)
.setZ(30)
.setMaxLines(3)
.apply();
DoraemonKit.install(application);
// H5任意门功能需要,非必须
DoraemonKit.setWebDoorCallback(new WebDoorManager.WebDoorCallback() {
@Override
public void overrideUrlLoading(Context context, String s) {
// 使用自己的H5容器打开这个链接
// 使用自己的H5容器打开这个链接
WebView webView = new WebView(context);
webView.loadUrl(s);
}
});
}
public static RefWatcher getRefWatcher(Context context) {
MyApplication application = (MyApplication) context.getApplicationContext();
return application.refWatcher;
}
@Override
public void onDestroy() {
super.onDestroy();
RefWatcher refWatcher = WanAndroidApp.getRefWatcher(_mActivity);
refWatcher.watch(this);
}
@Override
public void onDestroy() {
super.onDestroy();
RefWatcher refWatcher = WanAndroidApp.getRefWatcher(getActivity());
refWatcher.watch(this);
}
public static RefWatcher getRefWatcher(Context context) {
WanAndroidApp application = (WanAndroidApp) context.getApplicationContext();
return application.refWatcher;
}
public static RefWatcher getRefWatcher(Context context) {
SampleApplication application = (SampleApplication) context.getApplicationContext();
return application.refWatcher;
}
@Override
public void onDestroy() {
super.onDestroy();
RefWatcher refWatcher = AppHeap.getRefWatcher();
refWatcher.watch(this);
}
public static RefWatcher install(Application application) {
return RefWatcher.DISABLED;
}
public static RefWatcher getRefWatcher(Context context) {
DaintyApplication application = (DaintyApplication) context.getApplicationContext();
return application.refWatcher;
}
@Override
protected RefWatcher installLeakCanary() {
return LeakCanary.refWatcher(this)
.watchDelay(10, TimeUnit.SECONDS)
.buildAndInstall();
}