Android - memory leak or?

孤者浪人 提交于 2019-12-04 15:05:46

Sorry I can't help you on your Spinner problem but I can have a try on the second part:

Romain Guy post on android developer blog explain two important things.

First:

When you create a View (TextView, ImageView...) you must not create it with the activity Context

// DO NOT DO THIS
TextView label = new TextView(this);

Otherwise the View get a reference to your activity and will never be deallocated.

Instead, when you create a View programatically, you have to use the application context:

TextView label = new TextView(getApplicationContext());

Second:

When you link a Drawable to an View, it keeps a callback on your activity via the Context. If you leave it, it will leak memory when your activity is destroy.

The thing to do to avoid that is to "set stored drawables' callbacks to null when the activity is destroyed" so for example whith an ImageView:

protected void onDestroy() {
    imageView.getDrawable().setCallback(null);
    super.onDestroy();
}

You have to do the same for the background drawable...

Hope it helps.

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