In my app there is a WebView displaying Html content and a back button to let the user navigate back in the browser history. In the WebChromeClient, I get the Html page title in onReceivedTitle() and display it in my app.
The user is shown the first page and the first page title is displayed. The user can then click a hyperlink in the Html to get to the next page. The second page has a different title, and this is correctly displayed. The problem is if the user than clicks the Back button, the first page is diaplayed from the history but onReceivedTitle() is not called again. So my fist page is displayed with the wrong title from the second page.
How can I get the title of the page being that was loaded by the call to goBack()?
Here is some simplified code to demonstrate. Imagine a layout with a TextView, WebView, and Button. Also, imagine two Html files in the assets folder where pageone.html links to pagetwo.html and the two pages have different titles.
public class MyActivity extends Activity {
WebView webView;
TextView title;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView);
title = (TextView) findViewById(R.id.title);
webView.loadUrl("file:///android_asset/pageone.html");
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String titleText) {
super.onReceivedTitle(view, titleText);
title.setText(String.format("Title: %s", titleText));
}
});
final Button button = (Button) findViewById(R.id.backButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (webView.canGoBack()) {
webView.goBack();
}
}
});
}
}