gpt4 book ai didi

android - 它是android中的内存泄漏吗

转载 作者:行者123 更新时间:2023-11-30 01:56:22 25 4
gpt4 key购买 nike

我创建了具有两个 Activity 的简单应用程序,在两个 Activity 中都有一个链接到另一个 Activity 的按钮。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (intent != null) intent = null;
intent = new Intent(Two.this,MainActivity.class);
startActivity(intent);
}

在两个 Activity 中都有相同的代码。当我按下 Button 并按下 DDMS 中的 Cause GC 时,allocated 总是增长 25kb 这正常吗?

最佳答案

我不会将其称为内存泄漏,但概念很差。您正在做的是将 Activity 放在堆栈上,这当然会占用越来越多的内存。如果您只想打开上一个 Activity (这样就不会在每次调用startActivity(...) 时都创建一个新 Activity ),请使用:

Intent intent = new Intent(Two.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

如果您想了解有关任务和返回堆栈的更多信息,请访问 this guide .

关于android - 它是android中的内存泄漏吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32152375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com