Is this possible to check if currently my app is in background and home screen is launched
Asked
Active
Viewed 4,912 times
4 Answers
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
-
Good answer, but please also mention what is "apps" for complete answer. – Khawar Sep 23 '11 at 13:15
-
@Rony: I would certainly like to know what "apps" is :-D – While-E Jan 31 '12 at 06:21
-
Sorry: apps is result of "mActivityManager.getRunningTasks()" – While-E Jan 31 '12 at 06:38
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
-
Good answer, but this breaks down if the user has a third-party launcher in use. – Nathan Bird Aug 27 '18 at 21:04
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).

AnimatedClocks
- 49
- 3