demo. run it again - you will see getInstanceCount() increases leaking

Activities. repeat running demo couple of times. counter still

increases."

So the questions are:

Is Toast leaking Context objects ?

Is it a bug in Toast, in ApiDemos, in the documentation ?

Should we use Application Context instead ?

Let's try to find the answers using some Unit Tests as we have been investigating in previous articles in this blog.

ToastActivity

Let's recreate a oversimplified version of the Activity to display just the Toast depending on an extra parameter DISPLAY_TOAST in the Intent starting the Activity.

package com.codtech.android.training.toast;

import android.app.Activity;

import android.app.Application;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.widget.Toast;

public class ToastActivity extends Activity {

private static final String TAG = "ToastActivity";

public static final String USE_ACTIVITY_CONTEXT =

"com.codtech.android.training.toast.useActivityContext";

public static final String DISPLAY_TOAST =

"com.codtech.android.training.toast.displayToast";

private Context toastContext;

private boolean useActivityContext;

private boolean displayToast;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

final Intent intent = getIntent();

useActivityContext =

intent.getBooleanExtra(USE_ACTIVITY_CONTEXT, true);

displayToast =

intent.getBooleanExtra(DISPLAY_TOAST, true);

if ( useActivityContext ) {

toastContext = this;

}

else if ( displayToast ) {

toastContext =

getApplication().getApplicationContext();

}

}

/* (non-Javadoc)

* @see android.app.Activity#onPause()

*/

@Override

protected void onResume() {

super.onResume();

if ( displayToast ) {

Toast.makeText(toastContext,

"Sample Toast: " + getInstanceCount(),

Toast.LENGTH_SHORT).show();

}

else {

Log.d(TAG, "No toast displayed");

}

finish();

}

/* (non-Javadoc)

* @see android.app.Activity#onDestroy()

*/

@Override

protected void onDestroy() {

super.onDestroy();

Log.d(TAG, "Activity destroyed: " + this);

}

}

ToastActivity Tests

As usual, let's create our test to.

In this very particular case we want it to run several times specified by ToastActivityTests.N, 50 actually but you can change it if you like, to see if displaying a Toast or not change things in some way.

RepeatedTestSuite

This class implements repeated tests.

package com.codtech.android.training.toast.tests;

import junit.framework.Test;

import junit.framework.TestSuite;

public class ReapeatedTestSuite extends TestSuite {

public static Test repeatedSuite(Test test, int count) {

TestSuite suite = new TestSuite("Repeated");

// there's no RepeatedTest in android's junit

for (int i=0; i<count; i++) {

suite.addTest(test);

}

return suite;

}

}

RepeatedActivityContextWithToast

This test will run testSingleActivityContextWithToast starting the activity displaying the Toast several times