0

Which option is the best when Passing String to another activity and load it inside a Webview.

I have two Activities so far:

1) MainActivity

2) WebViewpop

The MainActivity has an Edittext with a code of

 String Message1 = txtEditor.getText().toString();

 intent.putExtra("Url",Message1);
 startActivity(intent);

and on my WebViewPop.class

    webView = (WebView)findViewById(R.id.webView);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String Message1 = extras.getString("Url");
        webView.loadUrl(Message1);
        webView.getSettings().setJavaScriptEnabled(true);
      }

my Problem with this code is that it seems it doesnt seem to load the string that has been passed from the Mainactivity.

I also tried other Paths like saving to Internal/External Storage then Load it. It Does work(External Storage one) inside the Emulator but when I try it using my Phone, it doesn't find the saved file. Any help would be appreciated. TIA

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
  • try to load directly without extra `getIntent().getStringExtra("Url")` and try to print this in log – Ashish Sharma Jun 30 '18 at 07:28
  • it does print in the Log window but not all of the string, im trying to send a long Javascript codes through String. and also loading directly with `getIntent().getStringExtra("Url")` didnt help – Hakkyu Kim Jun 30 '18 at 07:39
  • did you check this https://stackoverflow.com/questions/32163517/run-javascript-code-in-webview – Ashish Sharma Jun 30 '18 at 09:33
  • yeah i already saw it.and he's using static strings/codes for his webview but what im trying to accomplish is a abit different. i have a Edittext thats been set as a Code Editor ,when i click a certain button, it will the enterred code and view it inside a Webview. i already have a working but it only works inside the emulator,when i transfer it on my phone it doesnt work anymore. – Hakkyu Kim Jun 30 '18 at 10:33

1 Answers1

0

If you want to know if the problem come from the parameter in the intent, use a breakpoint near webView.loadUrl(Message1); (or a log).

But for me the syntax is getIntent().getString("id").

  • it seems that webview is null all the time during breakpoints. Also when i log the String that has been pass, it doesnt contain all of the codes/string that has been copied, maybe thats one of the reasons it is not showing in the the webview? Does String Message1 have a character limit? – Hakkyu Kim Jun 30 '18 at 08:03
  • The webview is waiting for an URL like "http://google.com". The other option is to save your code in a file and load it with a "webView.loadUrl("file:///android_asset/myweb.html");" – ThisIsAPseudo Jun 30 '18 at 08:53
  • yeah i have tried that and it is working via Saving on External Storage using Emulator, but when i use my phone as an Emulator, it doesnt create the file on my SD card, as for Using the Internal Storage, havent found a code thats works for me. thats why i am here – Hakkyu Kim Jun 30 '18 at 09:26
  • Did you manage the WRITE_EXTERNAL_STORAGE in manifest and code ? – ThisIsAPseudo Jun 30 '18 at 12:26
  • yeah i did manage to make it work, but it only works inside the emulator,when i used my phone as an emulator with an SD card. it doesnt work anymore. thats is why im trying to find another to do same thing. I cant make the Internal Storage one work on my path – Hakkyu Kim Jul 01 '18 at 07:33