How to check if activity is in foreground or in visible background?
This is what is recommended as the right solution:
The right solution (credits go to Dan, CommonsWare and NeTeInStEiN)
Track visibility of your application by yourself using
Activity.onPause, Activity.onResume methods. Store "visibility" status
in some other class. Good choices are your own implementation of the
Application or a Service (there are also a few variations of this
solution if you'd like to check activity visibility from the service).Example
Implement custom Application class (note the isActivityVisible() static method):
public class MyApplication extends Application {
public static boolean isActivityVisible() {
return activityVisible;
}
public static void activityResumed() {
activityVisible = true;
}
public static void activityPaused() {
activityVisible = false;
}
private static boolean activityVisible;
}
Register your application class in AndroidManifest.xml:
<application
android:name="your.app.package.MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name" >
Add onPause and onResume to every Activity in the project (you may
create a common ancestor for your Activities if you'd like to, but if
your activity is already extended from MapActivity/ListActivity etc.
you still need to write the following by hand):
@Override
protected void onResume() {
super.onResume();
MyApplication.activityResumed();
}
@Override
protected void onPause() {
super.onPause();
MyApplication.activityPaused();
}
In your finish()
method, you want to use isActivityVisible()
to check if the activity is visible or not. There you can also check if the user has selected an option or not. Continue when both conditions are met.
The source also mentions two wrong solutions...so avoid doing that.
Source: stackoverflow
Knowing if activity is running on foreground or background
Expanding on mco's answer, your GCMIntentService
needs to do something that will trigger work in a foreground activity of yours, if you have a foreground activity. Typically, you will do this by setting up your activities to respond to some sort of message in onResume()
and removing that in onPause()
.
"Some sort of message" could be:
- An
Intent
sent viaLocalBroadcastManager
- An
Intent
sent via the classicsendBroadcast()
- A message via a message bus, like Otto
- Etc.
How to detect when an Android app goes to the background and come back to the foreground
The onPause()
and onResume()
methods are called when the application is brought to the background and into the foreground again. However, they are also called when the application is started for the first time and before it is killed. You can read more in Activity.
There isn't any direct approach to get the application status while in the background or foreground, but even I have faced this issue and found the solution with onWindowFocusChanged
and onStop
.
For more details check here Android: Solution to detect when an Android app goes to the background and come back to the foreground without getRunningTasks or getRunningAppProcesses.
How to detect if any of my activity is front-most and visible to user?
I don't know that there's a method to get the currently displayed activity, but you could do something combining the Activity Lifecycle and a flag.
For the flag, if you've extended the Application class, that's probably a decent place to store it. For extending the application class, the top answer to this question has info. (d).
So probably keep track of the current active activity (or a flag that the activity is visible) in onResume/onPause or onStart/onStop depending on exactly what behavior you want.
Since you have multiple activities, you'll need a centroid place for storing the flag, which is why the Application makes sense. You can get the custom Application object by casting the application context (e.g. ((MyApplication)getApplicationContext()).isMyActivityActive).
You could extend Activity as well to help keep this code clean and contained.
If you're using a service you could bind to the service in every activity in the onStart/onStop (or onResume/onPause). If bound, you're visible.
Android: how do I check if activity is running?
You can use a static
variable within the activity.
class MyActivity extends Activity {
static boolean active = false;
@Override
public void onStart() {
super.onStart();
active = true;
}
@Override
public void onStop() {
super.onStop();
active = false;
}
}
The only gotcha is that if you use it in two activities that link to each other then onStop
on the first is sometimes called after onStart
in second. So both might be true briefly.
Depending on what you are trying to do (update the current activity from a service?). You could just register a static listener in the service in your activity onStart
method then the correct listener will be available when your service wants to update the UI.
Related Topics
How to Install the App Twice Without Interference in Android
Setting Onclicklistener for the Drawable Right of an Edittext
Error "Gradle Sync Failed: Connection Refused: Connect"
Not Opening Specific Activity on Notification Click When the App Is in Background/Not Running
Get Height and Width of a Layout Programmatically
Android Fastboot Devices Not Returning Device
Eclipse Compiles Successfully But Still Gives Semantic Errors
Html File Input in Android Webview (Android 4.4, Kitkat)
Html File Input Control With Capture and Accept Attributes Works Wrong
Android Navigationview: Reduce Space Between Icon and Text and 'Itembackground' Not Working
Flutter Keyboard Makes Textfield Hide
How to Add Many Spaces After the Text in Textview
How to Turn a Portable Sd Card into Internal Storage Via Adb Command
Remove Apk from Library in Google Play Developer Console
How to Display an Image in Full Screen on Click in the Imageview
How to Handle Notification When App in Background in Firebase