为什么EditText在Ice Cream Sandwich中保留其Activity的上下文 [英] Why does EditText retain its Activity's Context in Ice Cream Sandwich

查看:71
本文介绍了为什么EditText在Ice Cream Sandwich中保留其Activity的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ice Cream Sandwich中,当有一个包含EditText的Activity时,即使用户离开了Activity,EditText仍将保留Activity的Context.为了证明这一点,我创建了TestLeakActivity,它分配了一个大字节数组.由于从不对Activity的Context进行垃圾回收,因此字节数组会堆积在堆上,最终导致OutOfMemoryError.您可以使用DDMS堆工具观察堆的增长,并且可以通过在Eclipse MAT中查看HPROF文件来跟踪对EditText类的未完成引用.要创建内存泄漏,请进入LaunchActivity,然后继续启动和退出TestLeakActivity.

In Ice Cream Sandwich, when there's an Activity containing an EditText, the EditText will retain the Activity's Context even after the user leaves the Activity. To demonstrate this I've created TestLeakActivity, which allocates a large byte array. Since the Activity's Context is never garbage collected, the byte arrays accumulate on the heap, eventually causing an OutOfMemoryError. You can observe the heap growth by using the DDMS heap tool, and you can track the outstanding references to the EditText class by looking at the HPROF file in Eclipse MAT. To create memory leaks, go into LaunchActivity and just keep launching and backing out of TestLeakActivity.

LaunchActivity.java


package com.example.testleakproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class LaunchActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button button = new Button(this);
        button.setText("Start TestLeakActivity");
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LaunchActivity.this, TestLeakActivity.class);
                startActivity(intent);
            }
        });

        ViewGroup container = ((ViewGroup) findViewById(android.R.id.content));
        container.addView(button);
    }
}

TestLeakActivity.java


package com.example.testleakproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.EditText;

public class TestLeakActivity extends Activity {
    private byte[] mSomeBytes = new byte[1048576];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EditText editText = new EditText(this);
        editText.setHint("TestLeakActivity");

        ViewGroup container = ((ViewGroup) findViewById(android.R.id.content));
        container.addView(editText);
    }
}

推荐答案

这是一个已知的错误,将在ICS MR1中修复.

This is a known bug, that will be fixed in ICS MR1.

这篇关于为什么EditText在Ice Cream Sandwich中保留其Activity的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆