Android Startactivity From Javascriptinterface
Simple general question. Webview is hooked up to my JavascriptInterface class and it is most certainly functional. However, because JavascriptInterface does not extend Activity, I
Solution 1:
Just use mContext.startActivity(i)
;
No need to ever create Activity and no need to extend Activity, you just need a reference to Context which you already have stored.
Solution 2:
So i've a solution to start a new intent if have Fragments resp. SherlockFragments, i was struggling with this problem for almost an hour, but here u go:
Fragment opening the WebView:
publicclassMyWebViewFragmentextendsSherlockFragment {
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
Viewv= inflater.inflate(R.layout.webviewfragment, container, false);
WebViewmyWebView= (WebView) v.findViewById(R.id.webviewcontainer);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(newWebAppInterface(getActivity()),
"Android");
myWebView.loadUrl("file:///android_asset/javascript/index.html");
return v;
}
Javascriptinterface: I'm working on API Level 17, thats why i use the @JavascriptInterface. On id wouldnt work with L16 without the annotation, but i dont got why...
publicclassWebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */WebAppInterface(Context myWebViewFragment) {
mContext = myWebViewFragment;
}
@JavascriptInterface
publicvoidplayFragmentActivity(long id){
Intent intent = newIntent(mContext, FragmentActivity.class);
intent.putExtra(FragmentActivity.EXTRA_ID,Id);
mContext.startActivity(intent);
}
}
HTML
<body><div><inputtype="button"value="Start X FragmentActivity"onClick="changeFragmentActivity(183216076724928)" /></div><scripttype="text/javascript">functionchangeFragmentActivity(id) {
Android.playFragmentActivity(id);
}
</script></body>
Post a Comment for "Android Startactivity From Javascriptinterface"