I read from Romain Guy about avoid memory leak in android. I understand that I have to
use getApplicationContext()
to avoid mem leak. Anyway, my code is as follows:
MainMenuActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
canvasMenu = new CanvasMainMenu(this);
setContentView(canvasMenu);
}
CanvasMainMenu.java
public class CanvasMainMenu extends View {
private TextView textChallenge;
public CanvasMainMenu(Context context) {
super(context);
textChallenge = null;
textChallenge = new TextView(context);
textChallenge.setTypeface(fontJoan);
textChallenge.setTextColor(Color.WHITE);
textChallenge.setText("99");
textChallenge.measure((int)Define.getScreenWidth(), (int)Define.getScreenHeight());
textChallenge.layout(0, 0, 200, 200);
textChallenge.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
textChallenge.setDrawingCacheEnabled(true);
textChallenge.buildDrawingCache();
}
}
My question is, how can I avoid mem leak if my codes look like this.
Do I have to create static TextView
in file MainMenu.java?
Someone please explain me if I understand something wrong.