1

I'm working on an android app. I want to pass some value from my webview to my android application.

I solved this problem successfully by using the solution given in Passing data from java class to Web View html.

The problem that I'm facing is when I'm trying to call MainActivity methods from JavaScriptInterface.java, the methods are not being called or no errors are given.

here's what I have tried so far:

MainActivity act=new MainActivity();
act.myMethod() //This method is available in my activity

but it is not being called.

and

((MainActivity)getActivity).myMethod();

    public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme);
        setContentView(R.layout.activity_main);
    }
    public void myMethod() {
        Toast.makeText(this, "Inside MainActivity", Toast.LENGTH_SHORT).show();
    }
}

//JavaScriptInterface class

    class JavaScriptInterface {
    Context mContext;

    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void onButtonClick(String toast1, String toast2) {
        Toast.makeText(mContext, toast1+" | "+toast2, Toast.LENGTH_SHORT).show(); //THIS IS WORKING
        MainActivity act=new MainActivity();
        act.myMethod(); //NOT WORKING
    }
}
android developer
  • 114,585
  • 152
  • 739
  • 1,270
Anirban
  • 19
  • 1
  • 8

2 Answers2

0

Technically, the myMethod will get called, but since you create the MainActivity yourself, it's not attached to anything.

In general, you shouldn't create a new MainActivity instance this way. To open a new MainActivity, you use an Intent.

In your case, you should have a reference to the original MainActivity instance, and call this method there. Not create a new one in any way, as you already have it running.

An easy way to solve it:

MainActivity.this.myMethod("Hello there")

You don't have to store mContext . You already are inside of MainActivity.

So, the full code would be:

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView myWebView = findViewById(R.id.webview);
    myWebView.loadUrl("http://www.google.com");
    myWebView.addJavascriptInterface(new WebAppInterface(), "Android");
}

public void myMethod(String test){
    Toast.makeText(this, test, Toast.LENGTH_SHORT).show();

}

public class WebAppInterface {

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        MainActivity.this.myMethod("Hello there");
    }
}

}

In fact, I think you can even avoid having the MainActivity.this. , and call myMethod directly.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

You are creating another object of MainActivity this is why its not getting displayed. You are passing the activity context to the interface so you can just

((MainActivity)mContext).myMethod();

Sample MainActivity

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView myWebView = findViewById(R.id.webview);
    myWebView.loadUrl("http://www.google.com");
    myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
}

public void myMethod(String test){
    Toast.makeText(this, test, Toast.LENGTH_SHORT).show();

}

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        ((MainActivity)mContext).myMethod("hello");
    }
}

}

Check out android documentation for more info

hushed_voice
  • 3,161
  • 3
  • 34
  • 66
  • 1
    Since it's not a static class, but in fact a part of MainActivity, it's not needed to store `mContext`, as you can reach the MainActivity instance directly. – android developer Jan 28 '19 at 14:30