3

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.

PrvN
  • 2,385
  • 6
  • 28
  • 30
  • Have You tried to maintain singleton state among all activities? – sandrstar Mar 20 '13 at 14:03
  • 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) – Sebastian Aug 25 '15 at 12:52

4 Answers4

1

Maintain a boolean variable in Baseactivity,

i.e.:

  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.

Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
1

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()
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • im used ApplicationState.isActivityVisible() in on start and restart also but it always false . – PrvN Mar 20 '13 at 15:55
  • I am using this method.. and it is working fine. check whether you used activityresumed and activitypaused method currectly – stinepike Mar 20 '13 at 15:57
  • i used this methods in to my base activity class. and check isActivityVIsible in child onStart() and onRestart(). all case its false. – PrvN Mar 20 '13 at 16:06
  • you have to use these methods in all of your activities. else you wont be able to get exact result – stinepike Mar 20 '13 at 16:08
  • i have 20 -30 activities and all extends from Baseactivity.thats why i had implemented this in to base activity. please suggest ... – PrvN Mar 20 '13 at 16:10
  • ok .. just check whether your baseactivity is setting the data properly. – stinepike Mar 20 '13 at 16:11
  • i used flags for some activities Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP is causing problem ? – PrvN Mar 20 '13 at 16:19
  • 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. – mbonnin Jan 21 '15 at 10:54
  • you can check after onPause to see whether it was foreground or background. – stinepike Jan 22 '15 at 04:59
0

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

Community
  • 1
  • 1
Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

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.

user1666456
  • 351
  • 1
  • 9