0

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);
Kris
  • 189
  • 13

1 Answers1

0

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

Peter Strange
  • 2,640
  • 11
  • 11
  • Thanks Peter...!! I've tried the documentCreated method in BrowserFieldListener and this was giving some delay in showing the loading screen as it waits to get the connection established. Will try documentUnloading as well as you suggested. I've implemented caching also following this article http://supportforums.blackberry.com/t5/Java-Development/How-to-Implement-a-Web-Cache-for-Your-BrowserField2-Application/ta-p/817911 But still I'm not getting that much performance gain in loading. I hope this is the article you also referred to. – Kris Nov 21 '13 at 13:08
  • I've tried documentUnloading as well. But that event is not getting triggered. I've update my question to include the code snippet I used for that as well. Could you please check !! – Kris Nov 21 '13 at 13:59
  • Updated the question with that part as well..!! – Kris Nov 21 '13 at 14:52
  • I had gone through and tried this as well. Added this and debugged the code; but it was not going to the documentUnloading() method. But The documentCreated() and documentLoaded() method was getting triggered... I'm confused..!! :-( – Kris Nov 21 '13 at 15:40
  • Any idea why this is happening ? – Kris Nov 22 '13 at 12:53
  • No sorry, it works for me. There are issues with BrowserField implementation on some Simulators. Perhaps try another one? – Peter Strange Nov 23 '13 at 12:46