this question has been asked like a hundred times but all i could fine have the same answer using
getRunningAppProcesses()
which doesn't work properly on Lollipop
so it there a completely reliable way to know if my app is alive in the background?
Thank you
EDIT:
This's what i use to check if the app in the foreground:
the preferences used:
public static void setStartingActivity(Context context, boolean isStartingActivity) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("isStartingActivity", isStartingActivity).apply();
}
public static boolean isStartingActivity(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("isStartingActivity", false);
}
public static void setAppRunningInForeground(Context context, boolean isRunning) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("AppIsRunningInForeground", isRunning).apply();
}
public static boolean isAppRunningForeground(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("AppIsRunningInForeground", false);
}
starting the activity:
Utility.PreferencesManager.setStartingActivity(getApplicationContext(), true);
startActivity(new Intent(getApplicationContext(), MainActivity.class));
the onStop and onStart implementation:
@Override
protected void onStart() {
super.onStart();
Log.e("Debugging", "MainActivity onStart");
Utility.PreferencesManager.setAppRunningInForeground(getApplicationContext(), true);
}
@Override
protected void onStop() {
super.onStop();
Log.e("Debugging", "MainActivity onStop");
if (!Utility.PreferencesManager.isStartingActivity(getApplicationContext()))
Utility.PreferencesManager.setAppRunningInForeground(getApplicationContext(), false);
else
Utility.PreferencesManager.setStartingActivity(getApplicationContext(), false);
}