I am using a webview to load a remote mobile-optimized webpage (like m.mysite.com). The site uses jquery to load next 10 articles. I mean, by default only 10 articles are visible and on the bottom there is a button "load more". When a user presses it, it loads next 10 articles, and so on.
Now, when I load next 10 articles and want to read 15th or 16th article (just an example), the webview loads the article itself. but when I return via mobile Back button, the webview does not load to 15th or 16th article, but it reloads the page and I find myself on article 1 (e.g. it loads the default URL with first 10 articles only).
I tried this in the PC web browser, and it works fine in such browser.
How can I force webview to load the page from the cache, and not to reload it?
I use the usual code for back button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the BACK key and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
//gives the same problems
// #Override
// public void onBackPressed() {
// super.onBackPressed();
// webView.goBack();
// }
I found your question here, and saw it has no answer.
I am currently looking for the same thing, and I found this while researching and I am going to try it out: http://alex.tapmania.org/2010/11/html5-cache-android-webview.html
This should enable Cache on your webview. It's a matter of just copy and paste what is on the link above.
If you only want cache in the Back button, probably just keep it disabled and enable it when Back button is pressed?
Hope this helps you.
Related
My app is designed to be used outdoors (yachting) and displays a web page in a WebView (so I can use all the display area, fix in landscape, disable extraneous inputs like the BACK_KEY etc.).
In the web page, I want to capture the oncontextmenu event on an image like:
<img src="start_line_pin.png" width=55px
id="pinButton"
oncontextmenu = 'startLinePress("PIN"); return false'\>
When I open the page in my app's webview, a long press doesn't fire the event. Android doesn't seem to be passing the longpress event to the web page.
If I open the page directly in Chrome, my startLinePress function is called with a long press as I intended.
So, can anyone suggest how I get the longpress to be passed into the HTML in my WebView instead of it being handled by Android?
One of the most beneficial features of a forum such as this is that it makes you really think about your problem from another point of view.
The answer to my problem lies in the fact that I was trying to use an undocumented feature - the longClick on the web page invoking the oncontextmenu event.
The answer is to use the onLongClick event in java and then pass the event to the javascript function by using the WebView.loadUrl method. My WebView is contentView and the javascript function is javascript:startLinePress as follows:
contentView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
if(hr.getType() == 5){
contentView.loadUrl("javascript:startLinePress(\"ACTIVITY\")");
}
It needs a little more work to identify which element was clicked, by examining hr.getExtra() but you get the general idea.
Thanks stackoverflow for the great forum.
I have an issue in Android Jelly Bean version where the web view refuses input in text boxes.
Tried with a simple page with only one input tag as given below.
In android browser the page works fine, both keypress and blur events fire.
In Web view only the blur event fires and the text is not appearing.
I tried the webview settings mentioned in the link given below.
Why is Android WebView refusing user input?
There is a known defect in jelly bean for text fields with 'maxlength' attribute. I am not using max length .
<input id="phoneNumber"
name="phoneNumber" type="text" value="test" onblur="alert('lost focus');" onkeypress="alert('key press');"/>
Can anyone throw some light on how to resolve this issue??
I had the same problem and my research results with this:
As there is devices with no physical buttons, there is the navigation Bar with the relevant buttons.
So, if a physical 'back' button was pressed the onKeyDown method was called, and i guess they had to attach this method to the 'back' button on the Navigation Bar.
Any way, it results with the call to the onkeyDown and onkeyUp methods when the soft keyboard is being used, and if for some reason you override it you have to make sure you call super so it will be able to process the keys event:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// Do Somthing...
return true;
}
return super.onKeyDown(keyCode, event);
}
Hope it helps.
I'm developing a simple app on PhoneGap for an Android set top box.
I have an image that is usable as a link. When I connect a mouse to the set top box and click the image, the link works. But when I use the remote control and select the image (I see the border around the image so I know it is selected) and I click OK button, the link does not work.
How can I use the remote buttons in the code?
This is very tricky because Google didn't feel like mapping the keys on a remote to an actual key output.
To use the setTopBox, you're going to have to figure out what key codes your Android Set Top Box is using and modify the Activity's onKeyUp event to handle it. We currently have an example of a work-around in this bug however we don't have an agreed API for exposing these buttons to Javascript yet, which is why this bug is still open.
But in short, you'd do something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
{
sendJavascript("javascript:myJsMethod('UP');");
return true;
}
return super.onKeyDown(keyCode, event);
}
I'm developing an application right now and I need to disable the quick-search-box as somehow it dismisses my dialog boxes that request info to keep people who are not supposed to be using my application out [its in development, and its on the market - it makes it much easier to keep people up to date]. Listeners for all types of dismissing dialogs are never triggered - and I don't know why. I've looked everywhere and I get no result on how to disable this. (2.1 and up).
How to disable QSB..? was a good start, but it doesn't work. I don't know why google insists I use this... I have absolutely no need for this in my application.
How can I go about fixing this... or do I have to try another sort of DRM?
You can block the search on your activity or dialog by implementing this and returning false:
public boolean onSearchRequested() {
return false;
}
UPDATE:
Code works on dialogs, too
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0)
{
return true;
}
else
{
return false;
}
}
Returning true means that we are handling the Search Event.
I have an android application that loads web pages in an activity with a WebView. I am using the retrieving the page manually and using WebView's loadDataWithBaseURL to display it on screen. Everything there is fine.
Now, i am trying to override the Back button press to simulate going back in the WebView history stack. I am able to override the Back button press, i can see that there is a history stack in the WebView, i can see that the history url is correct, but when i call WebView's goBack() method, it displays a blank page.
Anyone encountered this before or give me a couple of suggestions to proceed from this?
Edit: If i use WebView's loadUrl method, the Back button with an override works as intended. But why.... If i need to handle this manually, how do i start messing with history pages?
I got the same problem also. I found that the problem went away if I set the historyUrl parameter on the call to loadDataWithBaseURL.
You should check if the canGoBack() method returns true before calling goBack()
The only solution I've found is to create a Stack<String> and manually manage history
The way I deal with this is keeping a local stack pointer to the number of loaded pages after the root page loaded using loadDataWithBaseURL . When going back, if my pointer hits 1 I am at root level and reload the root page with loadDataWithBaseURL.
FYI, I use this code in Activities with fragments, so the fragments implement the interface IBackButtonListener which helps me to capture the back button in the main activity and propagate the event to the current fragment. If the fragment returns true it means it has taken care of the event.
IBackbuttonListener.java
public interface IBackButtonListener {
public boolean onBackButtonPressed();
}
Fragment that implements IBackButtonListener and has a webview loaded from html data.
private int historyStackPointer = 0;
...
#Override
public boolean onBackButtonPressed() {
boolean rtn = false;
if (webView.canGoBack()) {
if(historyStackPointer > 1) {
webView.goBack();
historyStackPointer--;
rtn = true;
}else{
if(historyStackPointer == 1) {
// Reload the html data
webView.loadDataWithBaseURL("file:///android_asset/", html_data, "text/html", "UTF-8", null);
historyStackPointer = 0;
rtn = true;
}else{
webView.loadUrl("about:blank");
rtn = false;
}
}
} else {
rtn = false;
}
return rtn;
}
html_data is a String with the page's html.
What I noticed is that if the url ends in .html, that white screen appears when back button is pressed.
On the other hand, if you remove that .html from your url - obviously only if this is supported by that website (i.e. the redirection and all is handled properly at the server side and that it doesn't trigger the 404 Page Not Found error), that url will act as your base this time and when you press the back button, that white screen should not appear this time.
for example: you have to replace
http://example.com/page.html to:
http://example.com/page