I know how to call Java method in JavaScript code. It is done by @JavascriptInterface annotation. But what if I want to determine by Android which method from JS should be called? I'm calling an Android Dialog in JS using the mentioned annotation where I've got the switch statement which should determine which function should be called in JS. I used a flag which is not working because Dialogs are not synchronized so the method showDialog() is done before even Dialog starts. Is there any way to reach the Android-JS communication on the opposite side?
@JavascriptInterface
public int showDialog(){
new AlertDialog.Builder(this.activity)
.setTitle("Share iamge as...")
.setItems(new CharSequence[]{"Image", "PDF document", "Print"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){ // this switch should determine which JS function I call
case 0: chosenMethod = 0; // here should be called JS function (eg. exportImage() from JS code)
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;
}