Is it possible to check if the Android application goes to foreground? Maybe anything callback function? Any idea or suggestions is appreciated.
Asked
Active
Viewed 126 times
1
-
have you tried onResume() – Neji Apr 28 '14 at 05:33
-
hi please refer this http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not – Ando Masahashi Apr 28 '14 at 05:35
3 Answers
1
You can write code for callback activity in onRestart()
Method.

shweta_jain
- 453
- 1
- 5
- 19
-
`onRestart()` will fire if the application re-starts, but when Application comes from back to foreground, normally `onResume()` gets fire. – Lucifer Apr 28 '14 at 05:40
-
whenever you press back button and come on foreground activity that time onRestart() will be called you can try – shweta_jain Apr 28 '14 at 05:41
-
Agree, but this happens only if the activity has destroyed , not else. – Lucifer Apr 28 '14 at 05:42
-
http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not check this answer – Ando Masahashi Apr 28 '14 at 05:46
-
i checked again. onRestart() method called when you press back no matter onDestroyed() called or not. – shweta_jain Apr 28 '14 at 05:49
-
in your question you didn't mention using home button press. you mention callback option. – shweta_jain Apr 28 '14 at 05:59
-
i checked again when you press home button and again launch your app that time onRestart() and onResume() both method will be called. – shweta_jain Apr 28 '14 at 06:02
1
ActivityManager.getRunningTasks(1).get(0).yourActivity.getPackageName()
This method can get the foreground app package name.
you can use thread listen change.

Ando Masahashi
- 3,112
- 2
- 24
- 41

user3580042
- 11
- 1
0
As per android life cycle onResume
and onStart
will be called once your activity comes to foreground. For more details Android life cycle

Daud Arfin
- 2,499
- 1
- 18
- 37