I understand how to invoke a Java method within JavaScript code using the @JavascriptInterface
annotation. However, I am facing an issue when trying to determine which JS method should be called from Android. Currently, I am triggering an Android Dialog in JS with the mentioned annotation and have implemented a switch statement to decide which function to call in JS. Despite using a flag, the synchronization problem is causing the method showDialog()
to execute before the Dialog appears. Is there a solution for seamlessly managing bidirectional communication between Android and JavaScript?
@JavascriptInterface
public int showDialog(){
new AlertDialog.Builder(this.activity)
.setTitle("Share image as...")
.setItems(new CharSequence[]{"Image", "PDF document", "Print"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){ // This switch statement determines the JS function to call
case 0: chosenMethod = 0; // Here is where the JS function should be invoked (e.g., exportImage())
Log.v("Dialog onClick()", "Method chosen" + chosenMethod);
break;
case 1: chosenMethod = 1;
Log.v("Dialog onClick()", "Method chosen" + chosenMethod);
break;
case 2: chosenMethod = 2;
Log.v("Dialog onClick()", "Method chosen" + chosenMethod);
break;
}
}
})
.create().show();
Log.v("Dialog out of onClick", "Method chosen" + chosenMethod);
return chosenMethod;
}