Android EditText Memory Leak

拜拜、爱过 提交于 2019-11-30 07:31:33
Felix Zhang

I was confused about this memory leak for a very long time. But recently I found two ways to fix this problem.

  1. I found that if your TextView/EditText has the android:hint property, this cannot happen. So the easiest way is give every TextView/EditText the hint property.

  2. The most forceful way is to reflect over TextLine and find the ChangeWatcher listener, then kill this listener.

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!