Android 内部类 内存泄漏和上下文泄漏?
Android inner classes memory leak and leak by context?
我在启动画面中使用 Handler 延迟重定向到下一个 activity,如下所示..
@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);
}
在另一个 activity 中,我将上下文传递给 class 并将上下文保存在其中,如下所示 按钮单击 ..
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);
}
Handler
和 Runnable
不应以匿名方式使用。
- 您应该避免将整个 activity 作为参数传递给其他 类。如果上下文是您想要的全部,请使用
activity.getContext()
.
更多信息在这里:
http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html
http://www.androiddesignpatterns.com/2013/04/activitys-threads-memory-leaks.html
我在启动画面中使用 Handler 延迟重定向到下一个 activity,如下所示..
@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);
}
在另一个 activity 中,我将上下文传递给 class 并将上下文保存在其中,如下所示 按钮单击 ..
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);
}
Handler
和Runnable
不应以匿名方式使用。- 您应该避免将整个 activity 作为参数传递给其他 类。如果上下文是您想要的全部,请使用
activity.getContext()
.
更多信息在这里:
http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html
http://www.androiddesignpatterns.com/2013/04/activitys-threads-memory-leaks.html