diff --git a/src/com/android/email/EmailAddressAdapter.java b/src/com/android/email/EmailAddressAdapter.java index fbe78094f..e4d68707c 100644 --- a/src/com/android/email/EmailAddressAdapter.java +++ b/src/com/android/email/EmailAddressAdapter.java @@ -69,6 +69,15 @@ public class EmailAddressAdapter extends ResourceCursorAdapter { public Cursor runQueryOnBackgroundThread(CharSequence constraint) { String filter = constraint == null ? "" : constraint.toString(); Uri uri = Uri.withAppendedPath(Email.CONTENT_FILTER_URI, Uri.encode(filter)); - return mContentResolver.query(uri, PROJECTION, null, null, SORT_ORDER); + Cursor c = mContentResolver.query(uri, PROJECTION, null, null, SORT_ORDER); + // To prevent expensive execution in the UI thread + // Cursors get lazily executed, so if you don't call anything on the cursor before + // returning it from the background thread you'll have a complied program for the cursor, + // but it won't have been executed to generate the data yet. Often the execution is more + // expensive than the compilation... + if (c != null) { + c.getCount(); + } + return c; } } diff --git a/src/com/android/email/activity/MessageCompose.java b/src/com/android/email/activity/MessageCompose.java index 1084becc0..e8837e746 100644 --- a/src/com/android/email/activity/MessageCompose.java +++ b/src/com/android/email/activity/MessageCompose.java @@ -148,6 +148,8 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus private AsyncTask mSaveMessageTask; private AsyncTask mLoadMessageTask; + private EmailAddressAdapter mAddressAdapter; + private Handler mHandler = new Handler() { @Override public void handleMessage(android.os.Message msg) { @@ -375,6 +377,11 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus cancelTask(mLoadMessageTask); mLoadMessageTask = null; // don't cancel mSaveMessageTask, let it do its job to the end. + + // Make sure the adapter doesn't leak its cursor + if (mAddressAdapter != null) { + mAddressAdapter.changeCursor(null); + } } /** @@ -511,18 +518,18 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus mQuotedTextDelete.setOnClickListener(this); - EmailAddressAdapter addressAdapter = new EmailAddressAdapter(this); + mAddressAdapter = new EmailAddressAdapter(this); EmailAddressValidator addressValidator = new EmailAddressValidator(); - mToView.setAdapter(addressAdapter); + mToView.setAdapter(mAddressAdapter); mToView.setTokenizer(new Rfc822Tokenizer()); mToView.setValidator(addressValidator); - mCcView.setAdapter(addressAdapter); + mCcView.setAdapter(mAddressAdapter); mCcView.setTokenizer(new Rfc822Tokenizer()); mCcView.setValidator(addressValidator); - mBccView.setAdapter(addressAdapter); + mBccView.setAdapter(mAddressAdapter); mBccView.setTokenizer(new Rfc822Tokenizer()); mBccView.setValidator(addressValidator);