静态设置 onClickListener,内存泄漏?
我正在考虑创建一个可重用的函数,使视图可单击以关闭 - 在此示例中 RelativeLayoutAO
是背景。
final RelativeLayout rlAO = (RelativeLayout) findViewById(R.id.RelativeLayoutAO);
Utility.setOnClickFinish(rlAO);
然后在Utility类中:
public class Utility {
public static void setOnClickFinish(View view) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View clickedView) {
((Activity) clickedView.getContext()).finish();
}
});
}
}
这会导致内存泄漏吗?
I am thinking of making a reusable function to make views clickable to dismiss - in this example RelativeLayoutAO
is the background.
final RelativeLayout rlAO = (RelativeLayout) findViewById(R.id.RelativeLayoutAO);
Utility.setOnClickFinish(rlAO);
And then in the Utility class:
public class Utility {
public static void setOnClickFinish(View view) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View clickedView) {
((Activity) clickedView.getContext()).finish();
}
});
}
}
Would this cause a memory leak?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这不应该导致内存泄漏。
根据您问题的标题,我认为您可能误解了 static 关键字的含义。您没有“静态”设置任何内容。 setOnClickFinish() 被标记为静态的事实仅意味着它是一个类方法。
No, this should not cause a memory leak.
Based on the title of your question, I think it's possible that you are misunderstanding the implications of the static keyword here. You aren't setting anything "statically". The fact that setOnClickFinish() is marked as static simply means that it is a class method.