Android EditText Memory Leak-Collection of common programming errors

Alot of people are noticing EditText in an activity is holding a Strong Reference to an Activity even once its finished. To be clear this EditText is inside a layout and inflated, there is no Listeners set. This only happens on certain devices e.g. Samsung Galaxy S4 (Android 4.2.2) and others. Many post about this still no solution. First here is some useful posts. (Eventually GC will clean this so its not technically a leak, but for heavy memory apps it takes way to long and will cause OOM)

Android Samsung Memory leak in EditText

Why does EditText retain its Activity’s Context in Ice Cream Sandwich

EditText causing memory leak

Possibility of unhandled memory leak

The solutions noted do not work for all devices. It comes down to the Edittext Watcher. I think there may be solution in overriding this Watcher then having a function to clean it up onDestroy(). Please any help here, I been at this for days.

Here is the MAT Histogram

  1. Try to use Application Context instead of Activity Context in onCreateView() for this particular View (which contain any android:textIsSelectable=”true” components).

    // Singleton
    class MyApplication extends Application {
        private static MyApplication mApp;
    
        @Override
        public void onCreate() {
            mApp = this;
        }
    
        public static MyApplication getApp() {
            return mApp;
        }
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Suggested inflater use Activity Context
        // So we must tu use Application Context
        Context context = MyApplication.getApp().getApplicationContext();
        LayoutInflater myLayoutInflater = LayoutInflater.from(context);
    
        View view = myLayoutInflater.inflate(R.layout.my_view, container, false);
        return view;
    }