# NativeMessageBox::showMessageBoxAsync on Android is not topmost

**URL:** <https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999>\
**Category:** Android\
**Created:** [August 31, 2017, 9:23pm UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999 "2017-08-31T21:23:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nborthwick](https://avatars.discourse-cdn.com/v4/letter/n/43a26b/32.png) [@nborthwick](https://forum.juce.com/u/nborthwick)\
**Post date:** [August 31, 2017, 9:23pm UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999/1 "2017-08-31T21:23:34Z")

</div>

When you use NativeMessageBox::showMessageBoxAsync it pops the window on top of other components.  
However if you now click outside the messagebox window itself, the other component will obscure the messagebox and there is no way to get to the messagebox window anymore since its obscured.

Is there any way to prevent this from happening? Ideally message boxes should be always on top of all other windows. I understand that Android doesn’t have modal behavior but this is a major usability issue if messageboxes can get hidden from the user by accidentally clicking somewhere else!

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [September 6, 2017, 2:42pm UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999/2 "2017-09-06T14:42:15Z")

</div>

The messagebox is not hidden, it’s being dismissed. But in any case, there is a JUCE bug: dismissing the callback this way will never trigger the Callback in `NativeMessageBox::showMessageBoxAsync`. I’ve fixed this now on develop with commit [d4e0ba0](https://github.com/WeAreROLI/JUCE/commit/d4e0ba0e4d1dd80ad4f49e5cfd9ed4280f896d41). As it’s a change in java land, you’ll need to re-save your project with the Projucer. I did this for all the JUCE example projects (commit [ede622c](https://github.com/WeAreROLI/JUCE/commit/ede622c1effc9acfb09e2cb4b6c6960f4304c3cc)).

---

<div class="post-metadata">

**Author:** ![nborthwick](https://avatars.discourse-cdn.com/v4/letter/n/43a26b/32.png) [@nborthwick](https://forum.juce.com/u/nborthwick)\
**Post date:** [September 6, 2017, 4:16pm UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999/3 "2017-09-06T16:16:31Z")

</div>

Thanks Fabian. Yeah the callback was not being called. When its dismissed what is the default option selected for the messagebox and can it be specified? i.e. in a YesNo messagebox it should be No.  
Will try out the fix.

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [September 6, 2017, 4:57pm UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999/4 "2017-09-06T16:57:55Z")

</div>

> [@nborthwick](#):
>
> i.e. in a YesNo messagebox it should be No.

Yes that’s exactly what it will do.

---

<div class="post-metadata">

**Author:** ![nborthwick](https://avatars.discourse-cdn.com/v4/letter/n/43a26b/32.png) [@nborthwick](https://forum.juce.com/u/nborthwick)\
**Post date:** [September 11, 2017, 7:41pm UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999/5 "2017-09-11T19:41:05Z")

</div>

Hi Fabian,

I merged in your fix but it still doesn’t invoke the callback. We do this:

juce::NativeMessageBox::showYesNoBox(m\_iconType, m\_title, m\_message, m\_pMain,  
ModalCallbackFunction::create( MessageBoxMessageCallback, this) );

In android studio I see it calling the java function showOkCancelBox (it has your changes) but when you click outside the message box none of the onClick handlers get called. As a result it never executes the alertDismissed function with the callback.

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [September 12, 2017, 8:25am UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999/6 "2017-09-12T08:25:40Z")

</div>

Ahh you need to re-save your project with the Projucer for my change to have any effect (the Projucer copies the changed java files into your project when you save your jucer file).

This is the change that will call your callback when the MessageBox is dismissed:

> <https://github.com/WeAreROLI/JUCE/blob/develop/modules/juce_core/native/java/JuceAppActivity.java#L500>

---

<div class="post-metadata">

**Author:** ![nborthwick](https://avatars.discourse-cdn.com/v4/letter/n/43a26b/32.png) [@nborthwick](https://forum.juce.com/u/nborthwick)\
**Post date:** [September 12, 2017, 12:01pm UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999/7 "2017-09-12T12:01:40Z")

</div>

Hi Fabian,

Yes I have re-saved the project from the Projucer after including your changes. I also verified in the debugger that it was indeed seeing the new code. The problem is that the onCancel call itself in the code you quoted above is never invoked when you click outside the dialog. It does get called when you click the cancel button.

---

<div class="post-metadata">

**Author:** ![nborthwick](https://avatars.discourse-cdn.com/v4/letter/n/43a26b/32.png) [@nborthwick](https://forum.juce.com/u/nborthwick)\
**Post date:** [September 12, 2017, 12:02pm UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999/8 "2017-09-12T12:02:18Z")

</div>

I am running on a Pixel device with Android-O if that is relevant.

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [September 12, 2017, 2:36pm UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999/9 "2017-09-12T14:36:27Z")

</div>

Yeah tested this in the emulator with Android O and seems to work fine for me. Let me test this on a real device…

EDIT: yes, also works for me on a Nexus 5X with Android M

---

<div class="post-metadata">

**Author:** ![nborthwick](https://avatars.discourse-cdn.com/v4/letter/n/43a26b/32.png) [@nborthwick](https://forum.juce.com/u/nborthwick)\
**Post date:** [September 12, 2017, 7:29pm UTC](https://forum.juce.com/t/nativemessagebox-showmessageboxasync-on-android-is-not-topmost/23999/10 "2017-09-12T19:29:47Z")

</div>

My bad sorry. We had some local changes to that file so I missed merging in your code for the setOnCancelListener implementation. Thats why the callback wasn’t called. doh!  
Sorry about that. It works now. Thanks
