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
}
}