我正在尝试处理 Android 内存。问题是我想完全释放所有内存并开始新 Activity 。我找到了一些有用的答案:
Clear the entire history stack and start a new activity on Android
之后,我试着举两个例子:
第一个例子:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
我使用上面的代码在两个 Activity A 和 B 之间切换。这是内存消耗的图片:
第二个例子:
在主 Activity 中:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
在 Main2Activity 中:
onBackPressed();
看起来像 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK 并不是完全释放内存的完美方式。
我真的需要知道是否像第二个示例那样释放所有内存?
提前致谢!
最佳答案
将 android:noHistory="true"属性添加到您的 <activity>
在 AndroidManifest.xml 中像这样:
<activity android:name=".MyActivity"
android:noHistory="true">
</activity>
设置标志
Intent i = new Intent(this, Splash.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
并查看此链接 Changing activity in android clears the memory needed for the previous activities?
还有这个链接 https://commonsware.com/blog/2011/10/03/activities-not-destroyed-to-free-heap-space.html
关于java - Android:绝对清除 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713496/