本文介绍了Android内部类内存泄漏和上下文泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在启动画面中使用Handler将重定向推迟到下一个活动,如下所示..
I am using Handler in my splash screen for delaying redirection to the next activity as follows..
@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);
}
在另一个活动中,我将上下文传递给一个类,并将上下文保存在如下 按钮点击 ..
And in another activity am passing context to a class and holding the context inside that as follows on button click ..
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();
}
}
其中任何一个都是泄漏的原因?
Is any one of these will be a reason for leakage ?
如何使用处理程序如下..
And how about using handler as follows..
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);
}
推荐答案
-
处理程序
和Runnable
不应以匿名方式使用。 - 你应避免将整个活动作为参数传递给其他类。如果上下文是您想要的全部
activity.getContext()
。
Handler
andRunnable
should not be used in anonymous fashion.- You should avoid passing the whole activity as a parameter to other classes. If context is all that you want use
activity.getContext()
.
更多信息:
这篇关于Android内部类内存泄漏和上下文泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!