In a WebView, how do you access the JavaScriptInterface from multiple web
pages, and not just from the page that you load via mWebView.loadUrl("http://10.241.139.45:9081/amexco/login.html")
e.g.
In my project's assets folder, I have:
login.html
page1.html
page2.html
... and here is the relevant WebView code:
webView.addJavascriptInterface(new JavaScriptInterface(), "android");
webView.loadUrl("file:///android_asset/index.html");
So at different points in time, a user may look at index.html, page1.html, etc. From what I can tell, only the JavaScript in index.html has access to the JavaScriptInterface via "window.android.someMethodName()". When I try to make a call to "window.android.someMethodName()" from page1.html, it says that "window.android" is not defined.
so the user starts the webview from
login.html page and navigates through different pages..
my requirement is in the last page of my webpage i have a button saying return to android activity. so when user clicks on the last page of my navigation the user has to come back to the android activity.
window.android.someMethodName()" can be called in html page which works fine. but this method will work if i call this method only from the first page which i loaded ie from the login.html page only this method will work.. if i call this method from the last page of the webview naviagation this is not working.
Can any one help me out in this.
Get rid of the window.. If you are injecting a virtual global named android, you call methods on it as android.someMethodName().
Also, make sure that you are viewing your subsequent Web pages in the WebView. By default, links bring up the user's default Web browser, which will not have your injected virtual global.
Related
I have a WebView and i using WebView.goBack() when tap back button and i using WebView.goBackOrForward(x) in another place.
My question is that why WebView.goBackOrForward(x) loads visited web page again from internet but WebView.goBack() does not load visited web page again and shows previous web page quickly?
As in the doc:
goBack
Added in API level 1
void goBack ()
Goes back in the history of this WebView.
instead
goBackOrForward
Added in API level 1
void goBackOrForward (int steps)
Goes to the history item that is the number of steps away from the current item. Steps is negative if backward and positive if forward.
I assume that knowing exactly which page WebView is going to load make it faster as with goBack it always load the previous page. goBackOrForward need to calculate which page it need to load before loading it. I'm not sure about that, you should check the code.
I am trying to wrap a web application say www.xyz.com within a webview using shouldOverrideUrlLoading. I have two activity one that launches the application, this checks all the activity running and determines whether to launch new instance or not and other activity which wraps my application www.xyz.com in the webview.
I'm facing two problems:
When I press the sleep button and then again press it to open the device, or when the application is idle and went to sleep and I press the button to open the device, the application restarts (the webview restarts).
Also when link in the webapplication which opens someother site say for example www.abc.com doesn't work fine, it works for the first time and opens the site in external browser as desired, however on further click tries to open the site within the webview which is not desired.
I have googled this and found suggestion like the code below:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("abc")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return false;
}
}
However, in my case for the first time the site www.abc.com is opened in external browser but for the second time its open within the webview, i've debugged it in eclipse and found the url value comes the same in subsequent cliks .
1) For the WebView restart problem, you need to add the following lines of code to your activity that contains the WebView
android:launchMode="singleInstance"
android:alwaysRetainTaskState="true"
Also see tutorials on how to save state of WebView in Bundle.
2) For your external browser problem, try
url.equals("www.abc.com")
instead of
url.contains("abc")
Hope that solves your problem :)
Hi i am having a activity with a button, on click of the button it has to load a custom browser in a new activity not the default browser of android. and i need a way to exclude the history of the browser such that on back press it comes back to the previous activity without navigating to previous website. I am new to android and any ways to do this
You are probably just talking about the WebView
Link to the official tutorial
Note :
This onKeyDown(int, KeyEvent) callback method will be called anytime a
button is pressed while in the Activity. The condition inside uses the
KeyEvent to check whether the key pressed is the BACK button and
whether the WebView is actually capable of navigating back (if it has
a history). If both are true, then the goBack() method is called,
which will navigate back one step in the WebView history.Returning
true indicates that the event has been handled. If this condition is
not met, then the event is sent back to the system.
You can start by putting a WebView in an activity.
If you want to have a view which can browse the web in your app then make use of the WebView view.
Else if you want to create your own custom browser, then you have to make use Web engine WebKit present in the library layer of the android.
Previously when using a webview in android, clicking any link would force the "proper" browser to open and then web browsing would continue in that instance.
However this seems to no longer be the case. The following code results in all links staying inside the webview, but I actually want the links to launch a new browser instance. Did this change in 2.3 ?
Note I am asking the opposite of what most people ask (they ask how to keep all links inside the webview, I want them to launch outside)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView view = new WebView(this);
setContentView(view);
view.loadUrl("http://news.bbc.co.uk");
}
}
EDIT: Clarification - the first URL will load in the WebView and all subsequent clicks will open in a new browser. This is simplification from what I really want, but good enough.
Bascially the problem is, previously clicking a link would open a new browser session. Most people don't want that (hence the questions on here about it) but I do. However for some reason now it seems to load in the webview all the time (2.3 perhaps?)
Let me understand. Do you want to launch the first webpage in the same webview, while the other hyperlinks to go to the default browser, or you want the default browser itself to open for the first link clicked?
If it's the first case, I don't exactly know, unless you know some way to gather the link URL from the webview.
For the second instance, i.e. launching the default browser for any URL click, just skip (remove) this line: view.setWebViewClient(new WebViewClient() { });, which will open all the links in the default browser, and not the webview itself.
Houps, wrong answer, I misunderstood your question.
Need to delete...
my android app is simple client for web site. It opens site's url, using JavaScript and DOM enters username and password (predefined) and clicks "Log in" button. It uses WebView class.
Question: how during that process I could show some "ajax" loading image instead of showing webweiv on the same activity?
THank you!
You can set a WebViewClient to your webview and use the callbacks onPagestart and onPageFinished to show the progress.