Loading screen inside handleNavigationRequest in Browserfield Blackberry

170 Views Asked by At

Inside my application, I'm displaying a website using BrowserField. And when each link inside the site is selected, I need to show loading screen so that the user won't feel blank.

I was able to add the loading screen inside this method

public void documentCreated(BrowserField browserField,
                            ScriptEngine scriptEngine, Document document)

But the problem is only when connection is established, this method will be called and so there will be a delay before the loading screen is displayed.

So I tried implementing the ProtocolController and adding the loading screen inside this method

public void handleNavigationRequest(BrowserFieldRequest request)

But still, the loading screen is displayed after a small delay (same as when it was under documentCreated method)

This is my code snippet

public void handleNavigationRequest(BrowserFieldRequest request)
            throws Exception {

        if (!NetworkUtil.isNetworkAvailable()) {
            Dialog.inform(Strings.NETWORK_ERROR);
        } else {

            UiApplication.getUiApplication().invokeAndWait(new Runnable() {
                 public void run() {
                     BaseScreen.showLoadingProgress(Strings.LOADING);
                 }
             });

            InputConnection ic = handleResourceRequest(request);
            browserField.displayContent(ic, request.getURL());
        }
    }

I tried this outside the thread as well....Still the same is happening. For testing, I added a dialog inside this method and it was coming on the same time I'm clicking any link inside the site. Only this loading screen takes time to load.

Is there any way to make this happen ?

Also, the browser field is taking a bit longer to load the website compared to the native browser.

Am I missing something here ! Please help

I have tried the documentUnloading method as you suggested. But it is not getting triggered. Given below is the code snippet, could you please check what I'm doing wrong here...!!

protected void onUiEngineAttached(boolean attached) {
        if (attached) {

            BaseScreen.showLoadingProgress(Strings.LOADING);

        }
        super.onUiEngineAttached(attached);
    }

try {
                listener = new BrowserFieldListener() {

                    // Page starts loading...
                    public void documentCreated(BrowserField browserField,
                            ScriptEngine scriptEngine, Document document)
                             {

                        // show the loading screen
                        //showLoadingProgress(Strings.LOADING);
                    }

                    public void documentError(BrowserField browserField,
                            Document document)  {
                        hideLoadingProgress();
                        Dialog.inform(Strings.NETWORK_ERROR);
                    }

                    public void documentAborted(BrowserField browserField,
                            Document document) { 
                        hideLoadingProgress();
                        Dialog.inform(Strings.NETWORK_ERROR);
                    }

                    public void documentUnloading(BrowserField browserField,
                            Document document) {

                        BaseScreen.showLoadingProgress(Strings.LOADING);
                    }

                    // Page loaded
                    public void documentLoaded(BrowserField browserField,
                            Document document) {

                        // the document has loaded, hide loading popup ...
                        BaseScreen.hideLoadingProgress();
                    }

                };
            } catch (Exception ex) {
                Dialog.inform(Strings.NETWORK_ERROR);
            }

browserField.addListener(listener);
            // add the browser field to a ui manager or screen
            add(browserField);

            // request the content
            browserField.requestContent(URL);
1

There are 1 best solutions below

6
Peter Strange On

I do this using the BrowserFieldListener (see BrowserFieldListener.html). It is slightly counter intuitive, but I display the loading screen in documentUnloading(), and remove it in documentLoaded(). When I first populate the BrowserField I also push the loading screen, and when the screen with the BrowserField is closed, I make sure the loading screen is popped too. So not a pretty solution, but it works for me.

And yes, in general, the BrowserField is slower than the Browser. I have not found a way round it. However one significant aspect is caching. Look for information on creating your own cache for the BrowserField - there is Thread on here and a KB article on the BB Web site. Sorry can't find them atm, will update when I do.

Update

As found by the OP, the caching article is here.

Further Update

Just to clarify two things:

  1. You must associate the Listener with the BrowserField, using the addListener method.
  2. Assuming you do the usual requestContent() for the initial load of your BrowserField, you will need to push the loading screen yourself because the first method in listener that will be invoked (assuming it has worked of course), will be documentLoaded().

A sample demonstrating how to use the Listener is included here: listener sample