4

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 cannot seem to Intent to a new activity using startActivity(intent);

Should I extend Activity? Is there another method to intent to another activity? (in my very special case, im trying to intent to the YouTube app)

package com.privateized.moreprivate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

public class JavaScriptInterface {
    Context mContext;

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

    /** Show a toast from the web page */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }



    ///// vvvvv This no Worky vvvvv Just crashes ////////
    public void YouTube(String id) {
        Log.d("JS", "Launching YouTube:" + id);
        Activity activity = new Activity();
        String videoId = id;
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+videoId)); 
        i.putExtra("VIDEO_ID", videoId); 
        Log.d("JS", "Starting Activity");
        activity.startActivity(i);
    }

}
1
  • Activity activity = new Activity(); - don't ever do that. The Android Activity class is never meant to be directly instantiated in that way. The answer from sgarman should work for what you need. Commented Jan 10, 2012 at 19:25

2 Answers 2

6

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.

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

2 Comments

See thats the weird thing, because thats the first thing i tried, however Eclispe failed to list startActivity in the autocomplete list, and when i entered manually would tell me it was invalid, and subsequently wouldn't let me compile. So i went off on a tangent lol Seems to work now though, maybe Eclipse just needed to reboot :x Thanks guys!
I'm a big IntelliJ IDEA supporter, might want to check it out on your spare time. Has really good android integration.
0

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:

public class MyWebViewFragment extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    View v = inflater.inflate(R.layout.webviewfragment, container, false);
    WebView myWebView = (WebView) v.findViewById(R.id.webviewcontainer);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.addJavascriptInterface(new WebAppInterface(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...

public class WebAppInterface {
Context mContext;

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


@JavascriptInterface
public void playFragmentActivity(long id) {

    Intent intent = new Intent(mContext, FragmentActivity.class);
    intent.putExtra(FragmentActivity.EXTRA_ID,Id);
    mContext.startActivity(intent);
}

}

HTML

<body>
<div>
    <input type="button" value="Start X FragmentActivity"
        onClick="changeFragmentActivity(183216076724928)" />
</div>

<script type="text/javascript">

    function changeFragmentActivity(id) {
        Android.playFragmentActivity(id);
    }
</script>
</body>

Comments

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.