0

InputMethodManager is keeping a soft reference to a destroyed activity. The below is in my HPROF dump histogram.

  • android.view.inputmethod.InputMethodManager$ControlledInputConnectionWrapper @ 0x43b7f768 Native Stack
  • mInputConnection java.lang.ref.SoftReference @ 0x42b51da0
  • com.android.internal.widget.EditableInputConnection @ 0x43b7f738
  • mTextView, mTargetView android.support.v7.internal.widget.TintEditText @
  • mClipExMgr android.sec.clipboard.ClipboardExManager @ 0x434ee190
  • myActivity

It's probably an EditText. Do I need to remove listeners on views too, at the onDestroy of an activity? Or should I just ignore this?

Frank
  • 12,010
  • 8
  • 61
  • 78

1 Answers1

1

Listeners normally do not reference the views; the views of course reference listeners, and the listeners reference the enclosing object (usually, an activity). So, when views are not referenced anymore, they may be freed, then, unless still referenced, the listeners may be freed, then, unless still referenced, the enclosing activity-or-something may be freed. So there's no need to remove listeners unless (they reference views and are referenced from long-living data structures).

If an activity references the views, they may be freed together. So again no need to free the listeners.

OTOH, make sure that views are nor referenced from objects that must outlive the views.

And make sure that non-static runnables created by an activity stop when the activity goes off the screen. The running code is referenced, so the enclosing instance (of e.g. Activity) is referenced, so everything it references is referenced.

PS I hope you know that the right way to use a soft reference is:

MyActivity m = mySoftRef.get();
if (m != null) {
    doStuff(m.somefield);
}

and not

if (mySoftRef.get() != null) {
    // NOTE: mySoftRef may get set to null at this point !!!
    doStuff(mySoftRef.get().somefield);
}
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127