Skip to content

Instantly share code, notes, and snippets.

@jesselima
Forked from alexjlockwood/MainActivity.java
Created March 31, 2020 18:30
Show Gist options
  • Save jesselima/c6b55fa6fe881739d37a6f0f10a50e84 to your computer and use it in GitHub Desktop.
Save jesselima/c6b55fa6fe881739d37a6f0f10a50e84 to your computer and use it in GitHub Desktop.
Sample usage of the "android.app.Application.ActivityLifecycleCallbacks" class.
public class MainActivity extends Activity {
private final MyActivityLifecycleCallbacks mCallbacks = new MyActivityLifecycleCallbacks();
@Override
protected void onCreate(Bundle savedInstanceState) {
// Always register before calling into the super class.
getApplication().registerActivityLifecycleCallbacks(mCallbacks);
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
super.onDestroy();
// Always unregister after calling into the super class.
getApplication().unregisterActivityLifecycleCallbacks(mCallbacks);
}
public static class MyActivityLifecycleCallbacks implements ActivityLifecycleCallbacks {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.i(activity.getClass().getSimpleName(), "onCreate(Bundle)");
}
@Override
public void onActivityStarted(Activity activity) {
Log.i(activity.getClass().getSimpleName(), "onStart()");
}
@Override
public void onActivityResumed(Activity activity) {
Log.i(activity.getClass().getSimpleName(), "onResume()");
}
@Override
public void onActivityPaused(Activity activity) {
Log.i(activity.getClass().getSimpleName(), "onPause()");
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
Log.i(activity.getClass().getSimpleName(), "onSaveInstanceState(Bundle)");
}
@Override
public void onActivityStopped(Activity activity) {
Log.i(activity.getClass().getSimpleName(), "onStop()");
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.i(activity.getClass().getSimpleName(), "onDestroy()");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment