Search code examples
androidmemorymemory-leaksandroid-tabhost

InputMethodManager holds reference to the tabhost - Memory Leak - OOM Error


View hierarchy is as follows TabActivity -> ActivityGroups -> Activities.

Using MAT I found that TabWidget is referenced by TabHost which is referenced by InputMethodManager, hence TabWidget is leaked. On Subsequent launch of application OutOfMemory Error is thrown.

Similarly all my activities are also referenced by InputMethodManager. (After closing the application all my activitygroups, activities, tabactivity, tabhost and tabwidget are leaked!!)

On Properly finishing the application (hitting back key), following is shown in logcat

WARN/InputManagerService(99): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@44a87748 (uid=10052 pid=1463)

How to remove the reference from InputMethodManager...?

Thing I tried:

A. Called this method onDestroy of my TabActivity
1. myTabWidget.removeAllViews()
2. myTabWidger.invalidate()

No Luck!



Solution

  • Thing I tried: A. Called this method onDestroy of my TabActivity 1.myTabWidget.removeAllViews() 2.myTabWidger.invalidate()

    Of course, it will not work. Activities aren't views neither in MVC/MVP/MVVM nor in Android SDK classes hierarchy. android.app.Activity doesn't extend android.view.View

    My colleague had similar problem with memory leaks - he declared tabHost in TabActivity in static way (he wanted to access it from another activity, when he hadn't been familiar with pattern Observer). I think you've made something similar.

    And, at last, my question: why do you reference activities in InputMethodManager (though I don't understand how: it's final class) and not InputMethodManager in activities? If you want global focus point for InputMethodManager, I can advice you to put reference of it to Application class. We extend Application class (for example, HostApplication), in this facade we declare common stuff (SharedPreferences, for example). And in the activities we write:

    HostApplication application = (HostApplication) getApplication();
    

    Then we get useful common stuff from it.