Created
December 28, 2018 08:35
-
-
Save nhaarman/fd129c004f34ffd953a8db1789947870 to your computer and use it in GitHub Desktop.
MainActivity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nhaarman.android2sandbox; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.os.Handler; | |
public class MainActivity extends Activity { | |
static int i = 0; | |
static boolean done = false; | |
private Runnable message = new Runnable() { | |
@Override | |
public void run() { | |
startActivity(new Intent(MainActivity.this, MainActivity.class)); | |
} | |
}; | |
private Handler handler = new Handler(); | |
private int mI; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
i++; | |
if (savedInstanceState != null) { | |
mI = savedInstanceState.getInt("i", i); | |
} else { | |
mI = i; | |
} | |
System.out.println("MainActivity@" + Integer.toHexString(this.hashCode()) + " onCreate (" + mI + ")"); | |
if (i <= 21 && !done) { | |
handler.postDelayed(message, 100); | |
} else { | |
done = true; | |
} | |
} | |
@Override | |
public void onBackPressed() { | |
System.out.println("MainActivity@" + Integer.toHexString(this.hashCode()) + " onBackPressed"); | |
i--; | |
super.onBackPressed(); | |
} | |
@Override | |
protected void onDestroy() { | |
handler.removeCallbacks(message); | |
System.out.println("MainActivity@" + Integer.toHexString(this.hashCode()) + " onDestroy (" + mI + ")"); | |
super.onDestroy(); | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putInt("i", mI); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: