Android Memory Leaks InputMethodManager Solved

Main Activity is not garbage collected even after finish because it is referenced by InputMethodManager indirectly .

Many times we see that when an android app is closed (means the last activity is finished ), we get that the main activity is not garbage collected as is is referenced by InputMethodManager indirectly , mainly this issue comes when we use RecyclerView in the fragment associated with FragmentStatePagerAdapter in that activity , TreeObserver or something related with InputMethodManager being used.

Even after onDestroy() of that main activity is being called the app keeps the reference of that activity as it is referenced by InputMethodManager indirectly.

So, we cannot fix it, as it’s due the bug in Android Source code.

Link one

Link two

There can be some ways to fix it but we did not get the way to fix it . After some observations we realize that if any other activity is being called after that the finish of main activity the things is going perfect , no more Memory Leaks by InputMethodManager.

So we created the DummyActivity.java like this

package com.memory.test;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by amitshekhar on 06/05/16.
 */
public class DummyActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 500);
    }
}

And a theme in styles.xml and added that in Androidmanifest.xml

<resources>
  <!-- Customize theme for floating. -->
    <style name="AppTheme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>
</resources>

And

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.memory.test">
  
    <application/>
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DummyActivity"
            android:theme="@style/AppTheme.Transparent"
            android:screenOrientation="portrait" />
    </application>

</manifest>

And added the following code in MainActivity.java

// Write this in onBackPress of your MainActivity
public class MainActivity extends AppCompatActivity{
  
    @Override
    public void onBackPressed() {
       super.onBackPressed();
       // This is important : Hack to open a dummy activity for 200-500ms (cannot be noticed by user as it is for 500ms
       //  and transparent floating activity and auto finishes)
       startActivity(new Intent(this, DummyActivity.class));
       finish();
    }
  
}

In this way , Dummy activity is being called after the Main Activity but cannot be noticed as 500ms is too small and even the dummy activity is transparent , so no chance of any side-effect or being noticed.

LeakCanary (A powerful library for finding memory leaks) : The developers of LeakCanary has also mentioned to ignore this leaks (Link) as it’s a bug in android source code.

But this is the way that we found to fix the memory leak created by InputMethodManager in MainActivity.

Hope this helps for now.

Happy Bug Fixing For Now :)

Also, Let’s become friends on Twitter, Linkedin, Github, and Facebook.