teoREtik
teoREtik

Reputation: 7916

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 memmory.

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 enter image description here

LogCat output after clicked enter image description here

Upvotes: 3

Views: 1074

Answers (1)

Shlublu
Shlublu

Reputation: 11027

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.

Upvotes: 3

Related Questions