Created
January 2, 2016 08:52
-
-
Save baleen37/75dc97a64a614c4da139 to your computer and use it in GitHub Desktop.
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
public class MyApp extends Application { | |
private AppStatus mAppStatus = AppStatus.FOREGROUND; | |
public void onCreate() { | |
super.onCreate(); | |
registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks()); | |
} | |
public MyApp get(Context context) { | |
return (MyApp) context.getApplicationContext(); | |
} | |
public AppStatus getAppStatus() { | |
return mAppStatus; | |
} | |
// check if app is foreground | |
public boolean isForeground() { | |
return mAppStatus.ordinal() > AppStatus.BACKGROUND.ordinal(); | |
} | |
public enum AppStatus { | |
BACKGROUND, // app is background | |
RETURNED_TO_FOREGROUND, // app returned to foreground(or first launch) | |
FOREGROUND; // app is foreground | |
} | |
public class MyActivityLifecycleCallbacks implements ActivityLifecycleCallbacks { | |
// running activity count | |
private int running = 0; | |
@Override | |
public void onActivityCreated(Activity activity, Bundle bundle) { | |
} | |
@Override | |
public void onActivityStarted(Activity activity) { | |
if (++running == 1) { | |
// running activity is 1, | |
// app must be returned from background just now (or first launch) | |
mAppStatus = AppStatus.RETURNED_TO_FOREGROUND; | |
} else if (running > 1) { | |
// 2 or more running activities, | |
// should be foreground already. | |
mAppStatus = AppStatus.FOREGROUND; | |
} | |
} | |
@Override | |
public void onActivityResumed(Activity activity) { | |
} | |
@Override | |
public void onActivityPaused(Activity activity) { | |
} | |
@Override | |
public void onActivityStopped(Activity activity) { | |
if (--running == 0) { | |
// no active activity | |
// app goes to background | |
mAppStatus = AppStatus.BACKGROUND; | |
} | |
} | |
@Override | |
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { | |
} | |
@Override | |
public void onActivityDestroyed(Activity activity) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment