Main Activity is not garbage collected after destruction because it is referenced by InputMethodManager indirectly

后端 未结 3 1881
梦如初夏
梦如初夏 2020-12-04 22:23

I followed \"Avoiding Memory Leaks\" article from here.

However the proposed solution does not solve the leak problem. I tested this with android emulator on Window

3条回答
  •  眼角桃花
    2020-12-04 22:50

    It seems that calling InputMethodManager's methods 'windowDismissed' and 'startGettingWindowFocus' do the stuff.

    Something like this:

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        //fix for memory leak: http://code.google.com/p/android/issues/detail?id=34731
        fixInputMethodManager();
    }
    
    private void fixInputMethodManager()
    {
        final Object imm = getSystemService(Context.INPUT_METHOD_SERVICE);
    
        final Reflector.TypedObject windowToken
            = new Reflector.TypedObject(getWindow().getDecorView().getWindowToken(), IBinder.class);
    
        Reflector.invokeMethodExceptionSafe(imm, "windowDismissed", windowToken);
    
        final Reflector.TypedObject view
            = new Reflector.TypedObject(null, View.class);
    
        Reflector.invokeMethodExceptionSafe(imm, "startGettingWindowFocus", view);
    }
    

    Reflector's code:

    public static final class TypedObject
    {
        private final Object object;
        private final Class type;
    
        public TypedObject(final Object object, final Class type)
        {
        this.object = object;
        this.type = type;
        }
    
        Object getObject()
        {
            return object;
        }
    
        Class getType()
        {
            return type;
        }
    }
    
    public static void invokeMethodExceptionSafe(final Object methodOwner, final String method, final TypedObject... arguments)
    {
        if (null == methodOwner)
        {
            return;
        }
    
        try
        {
            final Class[] types = null == arguments ? new Class[0] : new Class[arguments.length];
            final Object[] objects = null == arguments ? new Object[0] : new Object[arguments.length];
    
            if (null != arguments)
            {
                for (int i = 0, limit = types.length; i < limit; i++)
                {
                    types[i] = arguments[i].getType();
                    objects[i] = arguments[i].getObject();
                }
            }
    
            final Method declaredMethod = methodOwner.getClass().getDeclaredMethod(method, types);
    
            declaredMethod.setAccessible(true);
            declaredMethod.invoke(methodOwner, objects);
        }
        catch (final Throwable ignored)
        {
        }
    }
    

提交回复
热议问题