From ab48881a2fb72df016f2a00bc107e5a35a411a9d Mon Sep 17 00:00:00 2001 From: Po Lu Date: Sat, 11 Feb 2023 10:26:34 +0800 Subject: [PATCH] Fix displaying popup menus from a menu entry on Android * java/org/gnu/emacs/EmacsActivity.java (EmacsActivity, onDestroy) (onWindowFocusChanged): Keep track of the last focused activity. * java/org/gnu/emacs/EmacsDialog.java (display1): Use it if there is no current focus. --- java/org/gnu/emacs/EmacsActivity.java | 13 ++++++++++++- java/org/gnu/emacs/EmacsDialog.java | 26 +++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/java/org/gnu/emacs/EmacsActivity.java b/java/org/gnu/emacs/EmacsActivity.java index 30eaf85805a..3156a144a0f 100644 --- a/java/org/gnu/emacs/EmacsActivity.java +++ b/java/org/gnu/emacs/EmacsActivity.java @@ -47,6 +47,9 @@ public class EmacsActivity extends Activity /* List of activities with focus. */ public static List focusedActivities; + /* The last activity to have been focused. */ + public static EmacsActivity lastFocusedActivity; + /* The currently focused window. */ public static EmacsWindow focusedWindow; @@ -215,6 +218,11 @@ public class EmacsActivity extends Activity manager.removeWindowConsumer (this, isMultitask || isFinishing ()); focusedActivities.remove (this); invalidateFocus (); + + /* Remove this activity from the static field, lest it leak. */ + if (lastFocusedActivity == this) + lastFocusedActivity = null; + super.onDestroy (); } @@ -223,7 +231,10 @@ public class EmacsActivity extends Activity onWindowFocusChanged (boolean isFocused) { if (isFocused && !focusedActivities.contains (this)) - focusedActivities.add (this); + { + focusedActivities.add (this); + lastFocusedActivity = this; + } else focusedActivities.remove (this); diff --git a/java/org/gnu/emacs/EmacsDialog.java b/java/org/gnu/emacs/EmacsDialog.java index 7d88a23c58f..bd5e9ba8ee7 100644 --- a/java/org/gnu/emacs/EmacsDialog.java +++ b/java/org/gnu/emacs/EmacsDialog.java @@ -230,11 +230,31 @@ public class EmacsDialog implements DialogInterface.OnDismissListener AlertDialog dialog; if (EmacsActivity.focusedActivities.isEmpty ()) - return false; + { + /* If focusedActivities is empty then this dialog may have + been displayed immediately after a popup dialog is + dismissed. */ + + activity = EmacsActivity.lastFocusedActivity; + + if (activity == null) + return false; + } + else + activity = EmacsActivity.focusedActivities.get (0); - activity = EmacsActivity.focusedActivities.get (0); dialog = dismissDialog = toAlertDialog (activity); - dismissDialog.show (); + + try + { + dismissDialog.show (); + } + catch (Exception exception) + { + /* This can happen when the system decides Emacs is not in the + foreground any longer. */ + return false; + } /* If there are less than four buttons, then they must be individually enabled or disabled after the dialog is -- 2.39.5