sandalone
sandalone

Reputation: 41769

How to load this URL into WebView?

There is a rather specific webpage loaded into WebView which URL is like http://www.site.com/mob/ (basically a mobile-optimized web page). This webpage display 25 articles only and on the bottom is a button "More articles".

When a user presses it, I catch URL http://www.site.com/Web/MobHomeItems.aspx?page=N (where N is 2, 3, 4...) and after that another 25 items have been loaded on the same screen.

Now, when I click on some article and go to article details, and later return to the page via the Back key, the WebView forgets how many articles have been loaded and simply loads the default page with 25 displayed articles. Imagine how frustrating this would be to a user if he came to 100th article.

I tried overriding many methods in WebClient and in WebChromeClient, but so far I have been unable to load N number of pages loaded via "More Articles" button. For example, I first thought this would help, but it did not.

        @Override
        public void onLoadResource(WebView view, String url) {

            //http://www.site.com/Web/MobHomeItems.aspx?page=2

            if (url.contains("?page=")) {

                //save this URL for later and on return from 
               // article details, pass it to LoadResource()

            super.onLoadResource(view, url);
        }

Then I tried similar approach with other method - basically remembering how many pages have been loaded on the main page, and then on return from article details, simply tell webview to load this URL.

Can anyone help me? How to append loaded pages to the main page? Should I use JavaScript here maybe?

PS. Loading mentioned URL http://www.site.com/Web/MobHomeItems.aspx?page=N does not help as it loads this concrete page into the WebView only, and it does not append this Nth page to the main page.

EDIT

As @Raghunandan asked, I do not have problems loading back to 1st page (?page=1). This is default when user presses Back button on article details. I want to load to the page where a user was before pressing article details. If he was on ?page=100, I want to load back to that page e.g. I want to have 25x100 articles open. Again, default is always "open 25 articles or ?page=1 or http://www.site.com".

Upvotes: 0

Views: 1643

Answers (2)

fengshihao
fengshihao

Reputation: 34

There is a another way to do this.

The WebSettings has a private method "setPageCacheCapacity" , the default value is 0 , you could enlarge it (may be 5).

You can access this method by using reflection of java.

The method can enable WebView to cache more than one document. In the other word. when user press the back key, the WebView will go back to the older document.

Upvotes: 0

fengshihao
fengshihao

Reputation: 34

Override the method shouldOverrideUrlLoading of WebViewClient.

like this:

public boolean shouldOverrideUrlLoading (WebView view, String url) {
   if (url is kind of article detail) {
       WebView newOne = new WebView(); // create a new Webview for displaying the details.
       view.setVisibility(View.INVISIBLE); // hiding current page (article list)  
       return true; // To tell the WebView we have process this url.
   } 
   return false;
}
  1. The user click one link of article's detail.
  2. shouldOverriderUrlLoading would be triggered.
  3. We created one new WebView to open the url.
  4. Hiding current page
  5. The user reading artical
  6. The user click back key, close the newOne WebView then make the previous WebView visible.The article list will show up immediately and remained the old statement

.

Upvotes: 1

Related Questions