Rony
Rony

Reputation: 345

How to check If I am currently on home screen

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

Upvotes: 3

Views: 5313

Answers (4)

Manu
Manu

Reputation: 379

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;
}

Upvotes: 3

Rony
Rony

Reputation: 345

if (apps.get(0).topActivity.getPackageName().equals("com.android.launcher")

this somehow solved my problem only for default home

Upvotes: 5

AnimatedClocks
AnimatedClocks

Reputation: 49

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).

Upvotes: 1

Romain Guy
Romain Guy

Reputation: 98501

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.)

Upvotes: 7

Related Questions