A simple component you can integrate into your Android app in order to detect if the app goes to background or foreground
This version of the library has no additional dependencies.
You need the create a customn Application
class for your application and register a listener that will listen to the activies life cycle throught the implementation of the ActivityLifecycleCallbacks
interface.
public final class CustomApplication
extends Application
implements ActivityLifecycleCallbacks
{
@Override
public void onCreate()
{
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
@Override
public void onTerminate()
{
super.onTerminate();
unregisterActivityLifecycleCallbacks(this);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState)
{
}
@Override
public void onActivityStarted(Activity activity)
{
}
@Override
public void onActivityResumed(Activity activity)
{
}
@Override
public void onActivityPaused(Activity activity)
{
}
@Override
public void onActivityStopped(Activity activity)
{
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState)
{
}
@Override
public void onActivityDestroyed(Activity activity)
{
}
}
Do not forget to update your AndroidManifest.xml
file in order to specify that tour application use a custom Application
class :
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smartnsoft.myapplication"
>
<application
android:name=".CustomApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
<!-- activities declaration -->
</application>
</manifest>
Your custom Application
class can also implement the OnVisibilityChangedListener
to be notify when the application goes to background or foreground :
public final class CustomApplication
extends Application
implements ActivityLifecycleCallbacks, OnVisibilityChangedListener
{
//...
@Override
public void onAppGoesToBackground(Context context)
{
}
@Override
public void onAppGoesToForeground(Context context)
{
}
}
Then, you have to declare a BackgroundDetectorHandler
and notify it each time your application detects that an Activity
is resumed or paused. This BackgroundDetectorHandler
takes a BackgroundDetectorCallback
with which one you can tune some parameters like :
- the default visibility in order to be notified or not the first time the app goes to foreground ;
- the
OnVisibilityChangedListener
.
public final class CustomApplication
extends Application
implements ActivityLifecycleCallbacks, OnVisibilityChangedListener
{
private BackgroundDetectorHandler backgroundDetectorHandler;
@Override
public void onCreate()
{
super.onCreate();
registerActivityLifecycleCallbacks(this);
backgroundDetectorHandler = new BackgroundDetectorHandler(new BackgroundDetectorCallback(BackgroundDetectorHandler.ON_ACTIVITY_RESUMED, this));
}
//...
@Override
public void onActivityResumed(Activity activity)
{
backgroundDetectorHandler.onActivityResumed(activity);
}
@Override
public void onActivityPaused(Activity activity)
{
backgroundDetectorHandler.onActivityPaused(activity);
}
@Override
public void onAppGoesToBackground(Context context)
{
Log.d("CustomApplication", "onAppGoesToBackground");
}
@Override
public void onAppGoesToForeground(Context context)
{
Log.d("CustomApplication", "onAppGoesToForeground");
}
}
When you use the droid4me framework, you do not have to listen for the activities life cycle through the ActivityLifecycleCallbacks
interface. In fact, you can directly use your ApplicationNameInterceptor
class that should implements of the ActivityController.Interceptor
interface.
In this class, you can directly declare a BackgroundDetectorHandler
that takes a BackgroundDetectorCallback
with which one you can tune some parameters like :
- the default visibility in order to be notified or not the first time the app goes to foreground ;
- the
OnVisibilityChangedListener
.
Note : In this exemple, the class directly implements the OnVisibilityChangedListener
interface.
public final class ApplicationNameInterceptor
implements ActivityController.Interceptor, OnVisibilityChangedListener
{
private BackgroundDetectorHandler backgroundDetectorHandler;
public ApplicationNameInterceptor()
{
backgroundDetectorHandler = new BackgroundDetectorHandler(new BackgroundDetectorCallback(BackgroundDetectorHandler.ON_ACTIVITY_RESUMED, this));
}
@Override
public void onAppGoesToBackground(Context context)
{
}
@Override
public void onAppGoesToForeground(Context context)
{
}
@SuppressWarnings("unchecked")
@Override
public void onLifeCycleEvent(final Activity activity, Object component, InterceptorEvent interceptorEvent)
{
if (component != null)
{
// It's a Fragment
//...
}
else
{
// It's an Activity
//...
}
}
}
Finally, you can notify your BackgroundDetectorHandler
instance each time your interceptor detects that an Activity
is resumed or paused :
public final class ApplicationNameInterceptor
implements ActivityController.Interceptor, OnVisibilityChangedListener
{
private BackgroundDetectorHandler backgroundDetectorHandler;
public ApplicationNameInterceptor()
{
backgroundDetectorHandler = new BackgroundDetectorHandler(new BackgroundDetectorCallback(BackgroundDetectorHandler.ON_ACTIVITY_RESUMED, this));
}
@Override
public void onAppGoesToBackground(Context context)
{
Log.d("ApplicationNameInterceptor", "onAppGoesToBackground");
}
@Override
public void onAppGoesToForeground(Context context)
{
Log.d("ApplicationNameInterceptor", "onAppGoesToForeground");
}
@SuppressWarnings("unchecked")
@Override
public void onLifeCycleEvent(final Activity activity, Object component, InterceptorEvent interceptorEvent)
{
if (component != null)
{
// It's a Fragment
//...
}
else
{
// It's an Activity
if(interceptorEvent == InterceptorEvent.onStart)
{
backgroundDetectorHandler.onActivityResumed(activity);
}
if(interceptorEvent == InterceptorEvent.onStop)
{
backgroundDetectorHandler.onActivityPaused(activity);
}
}
}
}
To add Background Detector to your project, include the following in your app module build.gradle
file:
implementation ("com.smartnsoft:backgrounddetector:${latest.version}")
Check this article (in french) if you want to learn more how the library works.
This library is available under the MIT license. See the LICENSE file for more info.
This library was proudly made by Smart&Soft, Paris FRANCE