1

My main activity has 2 buttons. One to transfer the text to Activity2 and other to transfer url to Activity2.

Code snippet for main activity to send url is as follows:

 else if (v.getId()==R.id.SendUrl){
                    String query = mEditText.getText().toString();
                    String url ="www.google.com/#q="+query;
                    Intent intent = new Intent(this, Activity2.class);
                    intent.putExtra(EXTRA_MESSAGE,Uri.parse(url));
                    startActivity(intent);

                }

The receiving activity class has the following code:

public class Activity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2);
    // get intent from HomeActivity

    Intent intent = getIntent();

    // get extra message
    String message = intent.getStringExtra(HomeActivity.EXTRA_MESSAGE);

    //render in textview
    TextView textView = (TextView)findViewById(R.id.viewtext);
    textView.setTextSize(40);
    textView.setText(message);

    WebView webView=(WebView)findViewById(R.id.webNavigator);
    webView.loadUrl(intent.getStringExtra(HomeActivity.EXTRA_MESSAGE));
    //show it in content view

}

The intended query does not load. Any suggestions?

miselking
  • 3,043
  • 4
  • 28
  • 39
Shawn
  • 19
  • 1
  • 3

2 Answers2

1

You need too pass the url as string as-

String query = mEditText.getText().toString();
                    String url ="http://www.google.com/#q="+query;
                    Intent intent = new Intent(this, Activity2.class);
                    intent.putExtra(EXTRA_MESSAGE,url);
                    startActivity(intent);

Do not use Uri.parse(); If you want to use the uri you can go by- following-

String uri = Uri.parse("http://...")
                .buildUpon()
                .appendQueryParameter("key", "val")
                .build().toString();

You can have a look on this also.

Add WebView Client to your webview as

webView.setWebViewClient(new MyWebViewClient());


And the MyWebViewClient class is an inner class as-

  private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view,url);
        }
    }
Community
  • 1
  • 1
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40
1

You pass a Uri here:

intent.putExtra(EXTRA_MESSAGE, Uri.parse(url));

And try to get a String here:

webView.loadUrl(intent.getStringExtra(HomeActivity.EXTRA_MESSAGE));

The returned String will always be null because the extra is not a String.

If you pass the url like this, you just have to change one line and the code should work:

intent.putExtra(EXTRA_MESSAGE, url);
ByteHamster
  • 4,884
  • 9
  • 38
  • 53