I created a public global variable, I assigned data it from a subclass and I want to use that data in the onCreate method. The problem is that the variable returns null in the onCreate method and returns the data in other subclass. Am just trying to get value from javascript and store it in Java variable in the onCreate method. I am relatively new to Java and android development. Any help is appreciated.
public class MainActivity extends AppCompatActivity {
// Initiallising the WebView
private WebView mywebView;
public String userData;
private GoogleApiClient client;
@SuppressLint("JavascriptInterface")
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Webview
mywebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.setWebViewClient(new MyWebViewClient());
mywebView.addJavascriptInterface(new JavascriptBridge(this), "AndUser");
mywebView.loadUrl("file:///android_asset/www/index.html");
Toast t = Toast.makeText(getApplicationContext(), userData+" Tesing..." , Toast.LENGTH_LONG);
t.show(); // returns null Testing...
}
public class JavascriptBridge{
Context mContext;
JavascriptBridge(Context c) {
mContext = c;
}
@JavascriptInterface
public String getUserDetails(String message){
userData = message;
return message;
}
}
}