JSInterface

mystik

Member
Licensed User
Longtime User
Here's what I am trying to accomplish... I have a webview control...when I rotate the device, the page goes blank. I need to be able to restore the state without refreshing the page. Can JSInterface do this?
 

mystik

Member
Licensed User
Longtime User
I'm not an expert on JSInterface. However I do know that you can get the values of all the Html controls with it. Save these values in Activity_Pause by using a Map or several variables.
You can then load the page and set the values back in PageFinished event.

The values are actually not why I want to do this. Here's why...

The pages are from an online game, and many if not most of the URLs have parameters on them. For example, the last action the user might have done was make a purchase in the shop. If I then reload the last URL, it will make that same purchase again. (or any other activity, such as attacking another player) So, I just want to reload the content that was on the page (including any text results that were there).
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

I don't think there is a solution for this.

As you said - reloading a page on orientation change is ok for static webpages but will cause problems with an online game or shopping cart.

There does seem to be a solution when developing with the Android SDK, look at this example WebView app:

B4X:
package example.webviewstate;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class MyWebView extends Activity {
    /** Called when the activity is first created. */
   WebView myWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        if(savedInstanceState!=null){
           myWebView.restoreState(savedInstanceState);
        } else {
            myWebView.loadUrl("http://google.co.uk/?q=basic4android");
        }
    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
       myWebView.saveState(savedInstanceState);

    }
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState){
       myWebView.restoreState(savedInstanceState);
       super.onRestoreInstanceState(savedInstanceState);
    }
}

See how the Android SDK gives access to the Bundle 'savedInstanceState'?
It's saved and then restored when the activity is re-created on device orientation change.

So that app loads and displays a Google search for 'basic4android'.
If i click a link in the search results and then rotate my device then the activity is re-created and the WebView loads the last URL i was viewing NOT the original Google search URL.

But i suspect that the WebView re-requests that page when the activity is re-created and so in your situation would have the same problems as using JSInterface in a B4A app to restore the WebView's last state.

If you look at my post here: http://www.b4x.com/forum/basic4andr.../9400-save-webview-html-file-2.html#post56406.

You can see that you can get the WebView to send the currently displayed page's HTML etc to a B4A Sub once the webpage has loaded.
I wonder if you save that HTML as a String then could you reload that exact HTML back into the WebView on orientation change using the WebView loadHtml method?
This might work but depends a lot on the content of the webpage - if the webpage contains stuff such as javascript generated elements then simply restoring the webpage HTML would probably not also restore dynamic content.

Martin.
 
Upvote 0
Top