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.
Maintain a boolean variable in Baseactivity,
i.e.:
Inside
onResume()
ofBaseactivity
makeisForeground = true
and insideonPause()
method ofBaseactivity
makeisForeground = false
and whenever you want to know the status,check that boolean variable and apply your further logic accordingly.
Another way to solve is to call
putExtra
on the intents which let the user navigate between the app's activities. IfonRestart
/onResume
does not receive this Extra, the app was just coming into foreground.to check whether your application is in background of foreground you can do the following.
Declare a class which will maintain the state
in the
onResume
method of every activity of your application calland in
onPause
method of every activity of your application callNow at anytime you can check the foreground/background state of your application by just calling
There's no framework-provided way to do this. I've described my own solution here: https://stackoverflow.com/a/14734761/1207921