活动中的Android内存泄漏

活动中的Android内存泄漏,android,memory-management,memory-leaks,eclipse-memory-analyzer,Android,Memory Management,Memory Leaks,Eclipse Memory Analyzer,我正在使用MAT工具测试我的应用程序,发现有内存泄漏,但在代码中找不到它。请帮我做同样的事 //第一项活动 package intent.sample; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickL

我正在使用MAT工具测试我的应用程序,发现有内存泄漏,但在代码中找不到它。请帮我做同样的事

//第一项活动

package intent.sample;

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

public class IntentA extends Activity implements OnClickListener {
    Handler handler = new Handler();
    private Intent i;
    private Button button;

    public void onClick(View src) {
        if (src == (View) button) {
            i = new Intent(this, IntentSampleActivity.class);
            handler.postDelayed(new Runnable() {

                public void run() {
                    startActivity(i);
                }
            }, 20000);
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        button = (Button) findViewById(R.id.screen2button);
        button.setOnClickListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        this.finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler = null;
        i = null;
        this.finish();
    }
}
//第二项活动

package intent.sample;

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

public class IntentSampleActivity extends Activity implements OnClickListener {

    private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) findViewById(R.id.screen1button);
        button.setOnClickListener(this);
    }

    public void onClick(View scr) {
        if (scr == (View) button) {
            Intent ii = new Intent(this, IntentA.class);
            startActivity(ii);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        this.finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.finish();
    }
}

我正在创建一个新线程并开始一项活动。这会创建活动类的新实例吗

在这段代码中,每次启动活动时,都会创建一个新实例。为了避免这种情况,我需要在活动的android manifest.xml文件中使用启动模式。启动模式可以是单个任务或单个实例


非常感谢。

我在Ondestroy方法中为我的处理程序分配了null,但我不确定启动的线程是否会使用同一实例创建新活动或创建新实例?你真的不应该从
onPause()
onStop()
Ondestroy()
调用
finish()
。它不仅打破了标准的应用程序生命周期,而且只有在活动已经完成的情况下才会调用
onDestroy()
。是的,这很好,但我相信它不会产生任何内存问题。如果我错了,请纠正我。你是在泄露runnable吗?我无法理解runnable是如何启动新活动的?