Android myth 1/2: Activity.onDestroy()

Please prove me wrong

There’s a common conception that onDestroymarks the end of an Android activity so that you need to cancel all your subscribers/asynctasks/whatever as soon as onDestroy fires.

I believe this is mainly due to the Fragments implementations and the dreaded IllegalStateException but I have always wondered whether you could maintain a strong reference to an activity after onDestroy and do UI stuff there.

So I did a little test there:

private Runnable mChangeContentViewRunnable = new Runnable() {
public void run() {
Log.d(TAG, "callback!");
setContentView(new TextView(MainActivity.this));

protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
setContentView(new FrameLayout(this));


public void onBackPressed() {
Log.d(TAG, "onBackPressed");

protected void onDestroy() {
Log.d(TAG, "onDEstroy");
new Handler().postDelayed(mChangeContentViewRunnable, 3000);

Turns out it didn’t crash… Am I missing something ?

Like what you read? Give Martin Bonnin a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.