3

Is this possible to check if currently my app is in background and home screen is launched

Rony
  • 345
  • 8
  • 18

4 Answers4

7

There is no API to know whether the home screen is showing. However you can know when your app is sent to the background using the various Activity lifecycle callbacks (onStop, etc.)

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
5
if (apps.get(0).topActivity.getPackageName().equals("com.android.launcher")

this somehow solved my problem only for default home

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Rony
  • 345
  • 8
  • 18
3

Try this function,

public boolean isUserIsOnHomeScreen() {
    ActivityManager manager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
    for (RunningAppProcessInfo process : processes) {
        if(process.pkgList[0].equalsIgnoreCase("com.android.launcher")) {
            return true;
        } else {
            return false;
        }           
    }
    return false;
}
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Manu
  • 379
  • 2
  • 16
1

Wether use onPause/onStop and onResume methods for activity purposes, or make advantage with an own implemented service for backround-processing (based on time-delays or messages/receivers).