Created
July 29, 2018 13:08
-
-
Save deyanm/04dc17cbba8ad2e9cfefb59e6f282635 to your computer and use it in GitHub Desktop.
Open/close app listener, using foreground and background 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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.deyanmarinov.customrecyclerviewscroll"> | |
<application | |
android:name=".CustomApp" | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
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
package com.example.deyanmarinov.customrecyclerviewscroll; | |
import android.app.Activity; | |
import android.app.Application; | |
import android.content.ComponentCallbacks2; | |
import android.content.res.Configuration; | |
import android.os.Bundle; | |
public class AppLifecycleHandler implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 { | |
private boolean appInForeground = false; | |
private LifeCycleDelegate lifeCycleDelegate; | |
public AppLifecycleHandler(LifeCycleDelegate lifeCycleDelegate) { | |
this.lifeCycleDelegate = lifeCycleDelegate; | |
} | |
@Override | |
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | |
} | |
@Override | |
public void onActivityStarted(Activity activity) { | |
} | |
@Override | |
public void onActivityResumed(Activity activity) { | |
if (!appInForeground) { | |
appInForeground = true; | |
this.lifeCycleDelegate.onAppForegrounded(); | |
} | |
} | |
@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) { | |
} | |
@Override | |
public void onTrimMemory(int level) { | |
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { | |
appInForeground = false; | |
lifeCycleDelegate.onAppBackgrounded(); | |
} | |
} | |
@Override | |
public void onConfigurationChanged(Configuration newConfig) { | |
} | |
@Override | |
public void onLowMemory() { | |
} | |
} | |
interface LifeCycleDelegate { | |
void onAppBackgrounded(); | |
void onAppForegrounded(); | |
} |
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
package com.example.deyanmarinov.customrecyclerviewscroll; | |
import android.app.Application; | |
import android.util.Log; | |
public class CustomApp extends Application implements LifeCycleDelegate { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
AppLifecycleHandler lifecycleHandler = new AppLifecycleHandler(this); | |
registerLifecycleHandler(lifecycleHandler); | |
} | |
@Override | |
public void onAppBackgrounded() { | |
Log.d("Awww", "App in background"); | |
} | |
@Override | |
public void onAppForegrounded() { | |
Log.d("Yeeey", "App in foreground"); | |
} | |
private void registerLifecycleHandler(AppLifecycleHandler lifeCycleHandler) { | |
registerActivityLifecycleCallbacks(lifeCycleHandler); | |
registerComponentCallbacks(lifeCycleHandler); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment