1

After creating a ‘register’ button in the webview, I want to navigate directly to a specific component when I click ‘func register’ in Kotlin.

Kotlin logic is structured like this.

// Android
inner class JavaScriptInterface
{
    @JavascriptInterface
    @SuppressWarnings("unused")
    fun register(data: String){
        navigate(
            ridday202208Ev.actionHaydxn2208EventToEventJoinForm()
        )
    }
}

This is javascript code.

 function register() {

  }

return (
 <button onClick={() => register()}></button>
)

When you click the register function in Javascript code, the webview recognizes the register function and wants to call navigate. I don't know how to deal with it.

2
  • If you want to call a javascript function, when clicking the button. You can just use <button onClick="register()"></button> Commented Aug 4, 2022 at 6:58
  • @Geshode You need to execute navigate in Kotlin by recognizing the register function Javascript register in the webview. Commented Aug 4, 2022 at 6:59

1 Answer 1

1

If you want JS to be able to execute kotlin code, you need to call addJavascriptInterface(JavaScriptInterface(), "Native") That will create an object in JS called native with all the functions your annotated on that class. Calling one of them will call directly to Kotlin or Java. Remember that you'd call Native.register(), not just register() (the object name is the second parameter to the call above, change it to whatever you want).

Remember that any function you call like this can only take null, String, or Number (int, double, etc) as parameters. You can't pass objects. You can't pass arrays (you kind of can, but you need to pass a Java array, at which point its easier to write in Java). You can't pass functions. You can work around those limitations (except functions) by passing JSON strings and serializing/deserializing them as needed.

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

4 Comments

Then, are you saying that only String and Number parameters can be used as argument values, and it is impossible to call a function even using the Javascript Interface?
Are you saying that you can simply give toast messages as argument values ​​in JavaScript, and that you cannot directly call a function?
@minsu123 It's impossible to pass a function as a parameter. You can pass a string to display in a. toast on the native side. But you couldn't pass a callback function.
Sir, is it really impossible to call navigate in android when the register() button is clicked?

Your Answer

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.