Leakcanary: Auto detect fragment leaks using LeakCanary.install()

Created on 22 Sep 2015  路  6Comments  路  Source: square/leakcanary

Quoting docs:

"LeakCanary.install() returns a pre configured RefWatcher. It also installs an ActivityRefWatcher that automatically detects if an activity is leaking after Activity.onDestroy() has been called."

Is it on the road map to do this for fragments as well. And later on have an API like:

LeakCanary.watchFragments(true);
LeakCanary.watchActivities(true);

I think it is fairly common to leak fragments like activities, so, this enhancement will be useful I think.
If it is on road map and not yet implemented. I can start working on it. Something that would be called FragmentRefWatcher.

Let me know if this is intentionally left out for some reason or if I miss anything.

Thanks

Most helpful comment

Is this possible now? See FragmentLifecycleCallbacks and support library

All 6 comments

There are no FragmentLifecycleCallbacks that can be used to watch all fragments like there are ActivityLifecycleCallbacks. So it isn't possible to watch fragments without explicitly adding the calling refWatcher.watch(this) in the fragments onDestroy method.

Which callbacks are you referring to ? Also, I think if a fragment leaks within an activity, that would leak the activity and be caught. Need to look at LeakCanary source though.

http://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html is what Leak Canary uses to watch for when an activitys onDestroy is called. There is no such thing for fragments, hence no way to watch them automatically without adding the call yourself to onDestroy in every fragment you would like watched (obviously using a base fragment class and doing it there is easiest).

Okay. I am closing this one then. Thanks.

Is this possible now? See FragmentLifecycleCallbacks and support library

There is a pull request for this now: #894

Was this page helpful?
0 / 5 - 0 ratings