From 4ae16efe33e26a2e3600e5639dfd94af77f80a8a Mon Sep 17 00:00:00 2001 From: albendz Date: Sun, 21 Jul 2019 19:46:45 -0700 Subject: [PATCH 1/5] Clear parent from customView if parent is not null --- app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java index e9765551ce..d1de43c4d6 100644 --- a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java +++ b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java @@ -5,6 +5,7 @@ import android.app.Dialog; import android.content.DialogInterface; import android.view.View; +import android.view.ViewGroup; import org.apache.commons.lang3.StringUtils; @@ -116,6 +117,8 @@ private static void showAlertDialog(Activity activity, final Runnable onNegativeBtnClick, View customView, boolean cancelable) { + if (customView != null && customView.getParent() != null) + ((ViewGroup) customView.getParent()).removeAllViews(); AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle(title); builder.setMessage(message); From 8cc1817f1c2642717f271e19b2b4c5b406fd3e55 Mon Sep 17 00:00:00 2001 From: albendz Date: Sun, 28 Jul 2019 16:53:12 -0700 Subject: [PATCH 2/5] Do not create a new dialog when a dialog with a custom view already exists on resume --- .../java/fr/free/nrw/commons/utils/DialogUtil.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java index d1de43c4d6..cdd6932272 100644 --- a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java +++ b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java @@ -117,8 +117,13 @@ private static void showAlertDialog(Activity activity, final Runnable onNegativeBtnClick, View customView, boolean cancelable) { - if (customView != null && customView.getParent() != null) - ((ViewGroup) customView.getParent()).removeAllViews(); + // If the custom view already has a parent, there is already a dialog showing with the view + // This happens for on resume - return to avoid creating a second dialog - the first one + // will still show + if (customView != null && customView.getParent() != null) { + return; + } + AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle(title); builder.setMessage(message); @@ -139,6 +144,10 @@ private static void showAlertDialog(Activity activity, } }); + builder.setOnDismissListener((DialogInterface dialogInterface) -> { + dialogInterface.dismiss(); + }); + AlertDialog dialog = builder.create(); showSafely(activity, dialog); } From c5ee65275c9c8ac7f3e4ba1df7f49d751a718cb0 Mon Sep 17 00:00:00 2001 From: albendz Date: Sun, 28 Jul 2019 16:57:36 -0700 Subject: [PATCH 3/5] remove debug code from dialog custom view parent investigation --- app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java index cdd6932272..0ce8ca9c9e 100644 --- a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java +++ b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java @@ -5,7 +5,6 @@ import android.app.Dialog; import android.content.DialogInterface; import android.view.View; -import android.view.ViewGroup; import org.apache.commons.lang3.StringUtils; @@ -144,10 +143,6 @@ private static void showAlertDialog(Activity activity, } }); - builder.setOnDismissListener((DialogInterface dialogInterface) -> { - dialogInterface.dismiss(); - }); - AlertDialog dialog = builder.create(); showSafely(activity, dialog); } From 23d982bc66e19fe8db52eed5ab86dbfdec57c4bd Mon Sep 17 00:00:00 2001 From: albendz Date: Thu, 31 Oct 2019 11:50:01 -0400 Subject: [PATCH 4/5] Add rest file --- .../free/nrw/commons/utils/DialogUtilTest.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 app/src/test/kotlin/fr/free/nrw/commons/utils/DialogUtilTest.kt diff --git a/app/src/test/kotlin/fr/free/nrw/commons/utils/DialogUtilTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/utils/DialogUtilTest.kt new file mode 100644 index 0000000000..e6b786d4c4 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/utils/DialogUtilTest.kt @@ -0,0 +1,21 @@ +package fr.free.nrw.commons.utils; + +import android.app.Activity +import fr.free.nrw.commons.utils.DialogUtil; +import org.junit.Test +import org.mockito.Mockito.mock + +public class DialogUtilTest { + + @Test + fun testNoAlertDialogCreatedWhenCustomViewHasParent() { + val activity = Activity() + DialogUtil.showAlertDialog(activity, "title", "message", null, null, null, false /** cancellable **/) + } + + @Test + fun testAlertDialogCreatedWhenCustomViewHasNoParent() { + val activity = Activity() + DialogUtil.showAlertDialog(activity, "title", "message", null, null, null, false /** cancellable **/) + } +} From 44247d0ca0e043210943b2937d59551be044e409 Mon Sep 17 00:00:00 2001 From: albendz Date: Thu, 31 Oct 2019 11:52:20 -0400 Subject: [PATCH 5/5] Didn't mean to add that test file --- .../free/nrw/commons/utils/DialogUtilTest.kt | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 app/src/test/kotlin/fr/free/nrw/commons/utils/DialogUtilTest.kt diff --git a/app/src/test/kotlin/fr/free/nrw/commons/utils/DialogUtilTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/utils/DialogUtilTest.kt deleted file mode 100644 index e6b786d4c4..0000000000 --- a/app/src/test/kotlin/fr/free/nrw/commons/utils/DialogUtilTest.kt +++ /dev/null @@ -1,21 +0,0 @@ -package fr.free.nrw.commons.utils; - -import android.app.Activity -import fr.free.nrw.commons.utils.DialogUtil; -import org.junit.Test -import org.mockito.Mockito.mock - -public class DialogUtilTest { - - @Test - fun testNoAlertDialogCreatedWhenCustomViewHasParent() { - val activity = Activity() - DialogUtil.showAlertDialog(activity, "title", "message", null, null, null, false /** cancellable **/) - } - - @Test - fun testAlertDialogCreatedWhenCustomViewHasNoParent() { - val activity = Activity() - DialogUtil.showAlertDialog(activity, "title", "message", null, null, null, false /** cancellable **/) - } -}