开发者

Activity instance remains in the memory after onDestroy()

I know that this topic has been already beaten enough, but I still don't understand completely if Android System has fine behavior in following case:

I created small app consists of two classes, here is the code:

Main.java

public class Main extends Activity {
    private Button bv;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bv = (Button) findViewById(R.id.hello_txt);

        bv.setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent i = new Intent(Main.this, Main2.class);
                    startActivity(i);
                }
            }
        );
    }  

}

Main2.java

public class Main2 extends Activity {

    private TextView countOfActivities; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        countOfActivities = new TextView(this);

        setContentView(countOfActivities);
        countOfActivities.setText("Count of Activities: " + getInstanceCount());
    }

}

When I clicked on the button from first activity several times, I get that even after pressing BACK button that should call second Activity's onDestroy() it's instance remains in the memmor开发者_StackOverflow社区y.

Only after creating about 35 instances next click let me know, that GC cleared the memmory. I just want to completely be sure that it is normal system's behavior.

Following pictures from Emulator and LogCat

Button clicked 10 times

Activity instance remains in the memory after onDestroy()

LogCat output after clicked

Activity instance remains in the memory after onDestroy()


Yes, the system works fine. When you press the back button, your activity is removed from the activity stack.
onDestroy() may have been called, this doesn't mean that the instance was actually unallocated from the memory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜