Issue
In my app I have main activity and child activities. An action on main activity opens a child activity. Each child activity loads web page that have various urls all pointing to server locations. I am able to navigate to all the urls but when I press back, I am not redirected to the previous page. Instead it exists and brings me to main activity. The back button action should navigate to last url location. How to do that?
Solution
You need to add each url to a List object and then use that object to navigate back.
private LinkedList<String> navigationHistory;
@Override
protected void onCreate(Bundle savedInstanceState) {
....
mWebView.setWebViewClient(new MyWebViewClient());
....
}
@Override
public void onBackPressed() {
if (navigationHistory.size() > 0) // Check if the url is empty
{
navigationHistory.removeLast(); // Remove the last url
if (navigationHistory.size() > 0) {
// Load the last page
mWebView.loadUrl(navigationHistory.getLast());
} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
if (!navigationHistory.contains(url)) // Check for duplicate urls
navigationHistory.add(url); // add each loading url to List
super.onPageFinished(view, url);
}
}
Answered By - Sandeep Yohans
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.