]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix displaying popup menus from a menu entry on Android
authorPo Lu <luangruo@yahoo.com>
Sat, 11 Feb 2023 02:26:34 +0000 (10:26 +0800)
committerPo Lu <luangruo@yahoo.com>
Sat, 11 Feb 2023 02:26:34 +0000 (10:26 +0800)
* 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
java/org/gnu/emacs/EmacsDialog.java

index 30eaf85805ab0051009ea77b10215bb756ac62a8..3156a144a0f355ac65d5246b7cbbfbec68d676ad 100644 (file)
@@ -47,6 +47,9 @@ public class EmacsActivity extends Activity
   /* List of activities with focus.  */
   public static List<EmacsActivity> 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);
 
index 7d88a23c58f8dad9559c7bc30d36283b5ef22fd2..bd5e9ba8ee7a57ec1636cb949cb9871054c28b0c 100644 (file)
@@ -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