PowenKo, Android Tutorial 091, System, Memory Leaks

August 25, 2011   ·   0 Comments

Memory Leaks

reference:

http://developer.android.com/resources/articles/avoiding-memory-leaks.html

 

 

經常犯的錯誤:

錯誤1.

@Override
protected void onCreate(Bundle state) {
  super.onCreate(state);
  
  TextView label = new TextView(this);
  label.setText("Leaks are bad");
  
  setContentView(label);
}

錯誤2.
因為當手機螢幕轉時,會重新呼叫onCreate,所請把
大的圖片放在onCreate 外面如下

private static Drawable sBackground;
  
@Override
protected void onCreate(Bundle state) {
  super.onCreate(state);
  
  TextView label = new TextView(this);
  label.setText("Leaks are bad");
  
  if (sBackground == null) {
    sBackground = getDrawable(R.drawable.large_bitmap);
  }
  label.setBackgroundDrawable(sBackground);
  
  setContentView(label);
}

請多用

 Context context=Context.getApplicationContext() or Activity.getApplication().
shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By

Tags: , , ,


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon