Listener to detect whether a view is at the front?
ProgressDialog
, AlertDialog
and DialogFragment
would lay their content on the Window
, not to your activity's/fragment's view hierarchy. Which means, that as soon as either of them is shown, the focus of Window
is changed. Hence, you can make use of ViewTreeObserver#addOnWindowFocusChangeListener()
API:
contentView.getViewTreeObserver().addOnWindowFocusChangeListener(
new ViewTreeObserver.OnWindowFocusChangeListener() {
@Override public void onWindowFocusChanged(boolean hasFocus) {
// Remove observer when no longer needed
// contentView.getViewTreeObserver().removeOnWindowFocusChangeListener(this);
if (hasFocus) {
// Your view hierarchy is in the front
} else {
// There is some other view on top of your view hierarchy,
// which resulted in losing the focus of window
}
}
});
android: how do I check if dialogfragment is showing
simply check if it's null
if(prev == null)
//There is no active fragment with tag "dialog"
else
//There is an active fragment with tag "dialog" and "prev" variable holds a reference to it.
Alternatively, you could check the activity the fragment prev
is currently associated with, however, make sure you ask that after you make sure it's not null or you'll get a NullPointerException. Like this:
if(prev == null)
//There is no active fragment with tag "dialog"
else
if(prev.getActivity() != this) //additional check
//There is a fragment with tag "dialog", but it is not active (shown) which means it was found on device's back stack.
else
//There is an active fragment with tag "dialog"
How to check if dialog is displayed or display multiple dialogs of the same type?
Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog(). You just have to keep a reference to the Dialogs you create in onCreateDialog().
How can I display a dialog on Currently visible activity on android?
Try this if it helps you:
1. Create a Activity
with transparent theme and no title
.
2. In onCreate()
define your alert dialog
.
3. Starting this activity from broadcastReceiver will show the alert dialog
.
Check if a dialog is displayed with Espresso
To verify if dialog appears you can simply check if View with a text that present inside the dialog is shown:
onView(withText("dialogText")).check(matches(isDisplayed()));
or, based on text with id
onView(withId(R.id.myDialogTextId)).check(matches(allOf(withText(myDialogText), isDisplayed()));
To click on dialogs button do this (button1 - OK, button2 - Cancel):
onView(withId(android.R.id.button1)).perform(click());
UPDATE
- I think is possible since Espresso has multi window support.
- Not sure about clicking outside the custom dialog view but for checking if it is displaying or not you have to create your custom matcher and check inside it.
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
Related Topics
How to Check If Data Is Inserted in Room Database
Prevent Screen Rotation on Android
How to Resize the Avd Emulator Window in Android Studio
Retrofit2 - How to Upload Image Using Retrofit in Multipart
How to Reduce the Gap Between Tab Layout Text and Its Indicator
Httpmessagenotreadableexception: Could Not Read Json: Unrecognized Field Using Spring for Android
Add Text to Image in Android Programmatically
How to Set the Preview Image in Videoview Before Playing
How to Use Typefacespan or Stylespan With a Custom Typeface
Best Practice For Instantiating a New Android Fragment
How to Fix Mainactivity Does Not Have a Navcontroller
How to Figure Out Which Sim Received Sms in Dual Sim Android Device
I Get Fragment Not Attached to a Context. What Context Need to Use
How to Make Primary Key as Autoincrement for Room Persistence Lib
Java.Lang.Runtimeexception: Canvas: Trying to Draw Too Large(144000000Bytes) Bitmap
How to Display a Loading Spinner While Getting Data from Firestore
Cannot Resolve Symbol Appcompatactivity - Support V7 Libraries Aren't Recognized
Adb Error (Error: Protocol Fault (Couldn't Read Status): Invalid Argument)