我遇到了以下内存泄漏示例
package com.justinschultz.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class LeakedDialogActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setMessage("This dialog leaks!").setTitle("Leaky Dialog").setCancelable(false).setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {}
});
AlertDialog alert = builder.create();
alert.show();
}
}
我不明白为什么它会在旋转时泄漏。我了解,当对话框仍在屏幕上时会创建一个新活动(包含对旧活动的引用)。假设您关闭对话框并再次旋转。对最旧活动的引用不应该消失,从而允许回收内存吗?