0

I'm using Fragments and one of my Fragment I have a webview. I want to add a image button that will let user to go back.

I have 4 Fragments and their names are Fragment1,Fragment2,Fragment3,Fragment4.

Also I have 4 layouts tab1.xml tab2.xml tab3.xml tab4.xml and there is 4 webview objects in all layouts.

When I click Fragment1, it opens tab1.xml and tab1.xml's webview1 opens www.site.com and user starts surfing inside webview1.

If user visits 2 or more pages in webview1 I want this user let can go back with a button.

How can I do this?

Adam Croth
  • 85
  • 2
  • 10
  • you want to go back one page in webview or from current fragment? – Gru Dec 20 '13 at 14:52
  • I described, when visitor open an fragment, this fragment's have a webview. Visitor can browse inside this webview and if user visited more than 1 page, user should go back previous web page with a button or androids back button – Adam Croth Dec 20 '13 at 14:54

1 Answers1

1

In your decalarations:

WebView web = null;

In your onCreateView;

// Prepare WebView.
web = (WebView) v.findViewById(R.id.htmlDisplay);
// Set WebView.
final WebSettings webSettings = web.getSettings();

// Enable JavaScript.
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

In your back button (not the physical one):

web.goBack()

If you want to use your physical back button:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(event.getAction() == KeyEvent.ACTION_DOWN)
    {
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(web.canGoBack() == true)
            {
                web.goBack();
            }
            else
            {
                finish();
            }
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.tab1,container, false); webView = (WebView) ll.findViewById(R.id.webView1); webView.setWebViewClient(new WebViewClient()); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setLoadWithOverviewMode(true); webView.getSettings().setUseWideViewPort(true); if(webViewBundle==null){ webView.loadUrl("http://www.site.com"); }else{ webView.restoreState(webViewBundle); } return ll; } – Adam Croth Dec 20 '13 at 15:04
  • Just to try, as per your code, put the last override I put in my answer (that for using your physical button). replace web with webView (to match your WebView name). Run, navigate, use the back button. – Phantômaxx Dec 20 '13 at 15:12
  • Thank you, I tried but it doesn't work. As I write in my first post, I have 4 webViews and just one Main Activity, I put onKeyDown function to MainActivity and I create webView in my Fragment1.java onCreateViews's inside. But when I click physical back button, it gives unfortunatelly App has stoppeg error. – Adam Croth Dec 20 '13 at 15:29
  • So add a button in Fragment1.java, under (over, where you are comfortable with) webView. In the onClick Method, put: webView.goBack(); and you should be done. (Remove the previously added override to the physical backbutton - it won't work in this way) – Phantômaxx Dec 20 '13 at 15:35
  • Thank you, it works with a button inside Fragment1.java. If I put an item inside optionsMenu for going back, Is it possible to reach Fragment1's webView element and go it back? – Adam Croth Dec 20 '13 at 15:50
  • Let me understand... do you want to reach something INSIDE the WebView? Maybe yes, but you have to hassle with Javascript, I think... – Phantômaxx Dec 20 '13 at 15:53
  • No, as I write, I have 4 Fragments and all fragments open different web pages. And all fragments webview's should be 100% width and 100% height. So I have no available space to put a back button. Then I decide to put back button on the menu bar. If user visiting Fragment1's webView, the back button on the menubar should Fragment1's webView go back, if user visiting Fragment2's webView, the back button on the menubar should Fragment2's webView go back. I hope, I make you understand :) – Adam Croth Dec 20 '13 at 15:57
  • Yes, now it's clear. So you have to point the right WebView from the menu item. Declare a **protected static WebView web = null;** in your main activity. In each of your fragments, set (assuming your main activity is called MainActivity) **MainActivity.web =** ... (the WebView in that layout - the one you do findViewById). In your onMenuItemSelected, set this line for the appropriate menu item: **web.goBack();** Now, you should be really done. – Phantômaxx Dec 20 '13 at 16:04
  • Also is it possible to hide back button on the top menu if there is no page to go back? – Adam Croth Dec 20 '13 at 16:22
  • I don't think so. Beacause after it's gone once, how do you set it back? – Phantômaxx Dec 20 '13 at 16:31