I'm making a message app and in order to set the status of the user, I have to keep track of the state of my application. When I run the apk, the ActivityLifecycleCallbacks won't trigger, no matter the state of the app. I have tried all the possible fixes that I have found so far but none of them worked out. This is the code:
public class AppState extends Application implements Application.ActivityLifecycleCallbacks {
@Override
public void onCreate() {
super.onCreate();
Log.e("AppState ", "onCreate");
registerActivityLifecycleCallbacks(this);
}
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
Log.e("onActivityCreated", activity.getLocalClassName());
}
@Override
public void onActivityStarted(Activity activity) {
setLastSeen(KEY_LAST_SEEN_ONLINE);
}
@Override
public void onActivityResumed(@NonNull Activity activity) {
Log.e("onActivityResumed", activity.getLocalClassName());
}
@Override
public void onActivityPaused(@NonNull Activity activity) {
Log.e("onActivityPaused", activity.getLocalClassName());
}
@Override
public void onActivityStopped(Activity activity) {
setLastSeen(String.valueOf(System.currentTimeMillis()));
}
@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
Log.e("omInstanceState", activity.getLocalClassName());
}
@Override
public void onActivityDestroyed(@NonNull Activity activity) {
Log.e("onActivityDestroyed", activity.getLocalClassName());
}
private void setLastSeen(String lastSeen) {
FirebaseDatabase.getInstance().getReference(KEY_COLLECTION_USERS)
.child(FIREBASE_USER.getUid())
.child(KEY_LAST_SEEN)
.setValue(lastSeen);
}
}
For the sake of clarifying, I have been reading the documentation and some other tweaks and none of them made reference to implementing code on some other class of my project so I assume that the problem would be within this class. Thank you for your time :)
Check if you register your AppState
application class in manifest :
<application
android:name=".AppState"
...