0

I have a native C function (with JNI of course) that should callback a java method to display a dialog but this dialog is never displayed.

This is how this works:

  1. C code calls a java method
  2. The java method logs something
  3. Then it calls showDialog(SOME_ID)
  4. The onCreateDialog(SOME_ID) is called
  5. The dialog is constructed using AlertDialog.Builder
  6. When I call AlertDialog alert = builder.create() the application just stops right there (without crashing or freezing) but only when this call starts from the C code.

The code is as follows:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case SOME_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Message");
        builder.setPositiveButton(...);
        builder.setNegativeButton(...);

        AlertDialog alert = builder.create();
        System.out.println("...Never called from JNI");
        return alert;
    }
    return null;
}

In 6 I mean that I log something before and after calling builder.create() but the log after the create method is never called so the method never returns and the dialog is never created. I have a button to trigger the dialog and, as expected, it works.

I did another test, I printed getApplicationContext() calling directly from onCreate() and from onCreateDialog() (called from C code) and it is the same, because one of the first things I thought was that the onCreateDialog() was being called from another Context or something.

Do you have any idea why the dialog is not displaying?

1 Answer 1

1

The problem was that when calling back from JNI the application wasn't on UI Thread so the dialog was never shown. To fix it I used a Handler (on Activity):

public final Handler dialogHandler = new Handler(){     
    public void handleMessage(Message msg){
        showDialog(SOME_ID);
    }   
};

So instead of directly call showDialog() I do it by calling dialogHandler.sendMessage().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.