我在我的閃屏使用處理器延遲重定向如下下一個活動..Android內部類內存泄漏和上下文泄漏?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entrance);
screenTimeOut();
}
private void screenTimeOut() {
/* New Handler to start the next screen
* and close this Entrance after some seconds.*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
initTracker();
/* Create an Intent that will start the Next-Activity. */
checkLoginStatus();
}
}, SPLASH_DISPLAY_LENGTH);
}
而在另一個活動正在傳遞上下文的一類並保持內部的上下文上按鈕如下點擊 ..
private Tools tools;
tools = new Tools(DetailsScreen.this, true);
工具
private Context _context;
private Fragment _fragment;
private Activity activity;
private String filePath = null;
private String camImagePath = null;
public Tools() {
}
public Tools(Context _context, boolean flag) {
this._context = _context;
this.activity = (Activity) _context;
initPath();
if (flag) {
createImageFile();
}
}
這些中的任何一個都會成爲泄漏的原因?
又有怎樣使用處理程序如下..
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entrance);
screenTimeOut();
}
private void screenTimeOut() {
/* New Handler to start the next screen
* and close this Entrance after some seconds.*/
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
initTracker();
/* Create an Intent that will start the Next-Activity. */
checkLoginStatus();
}
}, SPLASH_DISPLAY_LENGTH);
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
將'tools'即使'DetailsScreen'成品留? – shhp
http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html請檢查此問題http://stackoverflow.com/questions/10864853/when-exactly-is-it-leak - 安全使用匿名內部類,這也是 – Vaisakh
Nop ..工具聲明爲私人非靜態字段.. @ shhp – Stella