Created
November 13, 2017 00:04
-
-
Save fcaldarelli/c21d37d1feb029e6cc260ab62c2be992 to your computer and use it in GitHub Desktop.
Check background/foreground status for Android App
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
import android.app.Activity; | |
import android.app.Application; | |
import android.content.ComponentCallbacks2; | |
import android.content.res.Configuration; | |
import android.os.Bundle; | |
import android.util.Log; | |
/** | |
* Created by Fabrizio on 13/11/17. | |
*/ | |
public class ApplicationLifecycleHandler implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 { | |
private final String TAG = ApplicationLifecycleHandler.class.getSimpleName(); | |
private boolean isInForeground = false; | |
private int numActivities = 0; | |
private ApplicationLifecycleHandlerListener listener; | |
public ApplicationLifecycleHandler(ApplicationLifecycleHandlerListener listenerInput) | |
{ | |
super(); | |
this.listener = listenerInput; | |
} | |
@Override | |
public void onActivityCreated(Activity activity, Bundle bundle) { | |
} | |
@Override | |
public void onActivityStarted(Activity activity) { | |
if (numActivities == 0) { | |
// app went to foreground | |
Log.d(TAG, "app went to foreground"); | |
isInForeground = true; | |
if(this.listener!=null) | |
{ | |
this.listener.onApplicationLifecycleForegroundStatusChanged(isInForeground); | |
} | |
} | |
numActivities++; | |
} | |
@Override | |
public void onActivityResumed(Activity activity) { | |
} | |
@Override | |
public void onActivityPaused(Activity activity) { | |
} | |
@Override | |
public void onActivityStopped(Activity activity) { | |
numActivities--; | |
if (numActivities == 0) { | |
// app went to foreground | |
Log.d(TAG, "app went to background"); | |
isInForeground = false; | |
if(this.listener!=null) | |
{ | |
this.listener.onApplicationLifecycleForegroundStatusChanged(isInForeground); | |
} | |
} | |
} | |
@Override | |
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { | |
} | |
@Override | |
public void onActivityDestroyed(Activity activity) { | |
} | |
@Override | |
public void onConfigurationChanged(Configuration configuration) { | |
} | |
@Override | |
public void onLowMemory() { | |
} | |
@Override | |
public void onTrimMemory(int i) { | |
if(i == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN){ | |
Log.d(TAG, "app went to background"); | |
isInForeground = false; | |
numActivities = 0; | |
if(this.listener!=null) | |
{ | |
this.listener.onApplicationLifecycleForegroundStatusChanged(isInForeground); | |
} | |
} | |
} | |
public interface ApplicationLifecycleHandlerListener | |
{ | |
public void onApplicationLifecycleForegroundStatusChanged(boolean isInForeground); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment