Created
September 30, 2015 16:25
-
-
Save plackemacher/a645532b2ceefdde95e7 to your computer and use it in GitHub Desktop.
Robolectric Activity Leak Fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressLint("NewApi") | |
@After | |
public void resetWindowManager() { | |
Class clazz = ReflectionHelpers.loadClass(getClass().getClassLoader(), "android.view.WindowManagerGlobal"); | |
Object instance = ReflectionHelpers.callStaticMethod(clazz, "getInstance"); | |
// We essentially duplicate what's in {@link WindowManagerGlobal#closeAll} with what's below. | |
// The closeAll method has a bit of a bug where it's iterating through the "roots" but | |
// bases the number of objects to iterate through by the number of "views." This can result in | |
// an {@link java.lang.IndexOutOfBoundsException} being thrown. | |
Object lock = ReflectionHelpers.getField(instance, "mLock"); | |
ArrayList<Object> roots = ReflectionHelpers.getField(instance, "mRoots"); | |
//noinspection SynchronizationOnLocalVariableOrMethodParameter | |
synchronized (lock) { | |
for (int i = 0; i < roots.size(); i++) { | |
ReflectionHelpers.callInstanceMethod(instance, "removeViewLocked", | |
ReflectionHelpers.ClassParameter.from(int.class, i), | |
ReflectionHelpers.ClassParameter.from(boolean.class, false)); | |
} | |
} | |
// Views will still be held by this array. We need to clear it out to ensure | |
// everything is released. | |
ArraySet<View> dyingViews = ReflectionHelpers.getField(instance, "mDyingViews"); | |
dyingViews.clear(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is in Android M??