Title: How to get callback when Android app comes foreground from background?
Tags: android;android-activity;background
Question: How to know app state background to foreground in android?
I had extends my activities from one Baseactivity call and Baseactivity class extends android Activity.
I put code appcomeForeground() into base activity on onRestart() but its call when we navigate activity into our foreground app also.
Please suggest way to get call back only when app comes foreground.
Thanks in advance.
Comment: Have You tried to maintain singleton state among all activities?

Comment: possible duplicate of [Checking if an Android application is running in the background](http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background)

Here is the accepted answer: to check whether your application is in background of foreground you can do the following.
Declare a class which will maintain the state
public class ApplicationState {
public static boolean isActivityVisible() {
return activityVisible;
public static void activityResumed() {
activityVisible = true;
public static void activityPaused() {
activityVisible = false;
private volatile static boolean activityVisible;
in the onResume method of every activity of your application call
ApplicationState.activityResumed()
and in onPause method of every activity of your application call
ApplicationState.activityPaused()
Now at anytime you can check the foreground/background state of your application by just calling
ApplicationState.isActivityVisible()
Comment for this answer: I am using this method.. and it is working fine. check whether you used activityresumed and activitypaused method currectly
Comment for this answer: you have to use these methods in all of your activities. else you wont be able to get exact result
Comment for this answer: ok .. just check whether your baseactivity is setting the data properly.
Comment for this answer: you can check after onPause to see whether it was foreground or background.
Comment for this answer: im used ApplicationState.isActivityVisible() in on start and restart also but it always false .
Comment for this answer: i used this methods in to my base activity class.
and check isActivityVIsible in child onStart() and onRestart().
all case its false.
Comment for this answer: i have 20 -30 activities and all extends from Baseactivity.thats why i  had implemented this in to base activity.
please suggest ...
Comment for this answer: i used flags for some activities Intent.FLAG_ACTIVITY_CLEAR_TOP        | Intent.FLAG_ACTIVITY_SINGLE_TOP
is causing problem ?
Comment for this answer: this does not give a callback. i.e. you can know the state at any time but you have no event when your app comes to foreground.

Here is another answer: There's no framework-provided way to do this. I've described my own solution here: https://stackoverflow.com/a/14734761/1207921

Here is another answer: Another way to solve is to call putExtra on the intents which let the user navigate between the app's activities. If onRestart/onResume does not receive this Extra, the app was just coming into foreground.

Here is another answer: Maintain a boolean variable in Baseactivity,
private boolean  isForeground;
Inside onResume() of  Baseactivity make isForeground = true
and inside onPause() method of Baseactivity make isForeground = false
and whenever you want to know the status,check that boolean variable and apply your further logic accordingly.
Comment for this answer: Thanks for reply.
But this is not working.
isForeground remains false. :(
Comment for this answer: onRestart im checking isForeground flag but its returns false.
Comment for this answer: try,private static boolean isForeground;
Comment for this answer: by adding log.d("Baseactivity","onRestart called"); inside onRestart() method of Baseactivity make sure it is being executed everytime.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:03/20/13', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<android><android-activity><background>', 'Title': 'How to get callback when Android app comes foreground from background?', 'ViewCount': 6626, 'id': 15525711, 'user_AboutMe': '', 'user_AccountId': 254152.0, 'user_DisplayName': 'PrvN', 'user_Reputation': 2335.0, 'user_Views': 153.0, 'user_id': 533346.0}}