Last active
August 29, 2015 14:19
-
-
Save jlmcdonnell/def876a8a364eeaffb37 to your computer and use it in GitHub Desktop.
Android - Better Activity lifecycle callbacks
This file contains 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
/** | |
* This class allows you to listen to when the user is entering the background (i.e. after a home button press, | |
* or opening recent apps etc) and when the user resumes the application from the background. | |
* | |
* @author John McDonnell | |
*/ | |
public class BetterActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks { | |
private int mForegroundActivities; | |
private boolean mHasSeenFirstActivity; | |
private boolean mChangingConfiguration; | |
@Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | |
} | |
@Override public void onActivityStarted(Activity activity) { | |
mForegroundActivities++; | |
if (mHasSeenFirstActivity && mForegroundActivities == 1 && !mChangingConfiguration) { | |
applicationDidEnterForeground(activity); | |
} | |
mHasSeenFirstActivity = true; | |
mChangingConfiguration = false; | |
} | |
@Override public void onActivityResumed(Activity activity) { | |
} | |
@Override public void onActivityPaused(Activity activity) { | |
} | |
@Override public void onActivityStopped(Activity activity) { | |
mForegroundActivities--; | |
if (mForegroundActivities == 0) { | |
applicationDidEnterBackground(activity); | |
} | |
mChangingConfiguration = activity.isChangingConfigurations(); | |
} | |
@Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { | |
} | |
@Override public void onActivityDestroyed(Activity activity) { | |
} | |
/** | |
* One day we'll be as cool as iOS | |
*/ | |
protected void applicationDidEnterForeground(Activity topActivity) { | |
} | |
protected void applicationDidEnterBackground(Activity lastActivity) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment