Dec 9, 2014

How to use back key in webview of fragment



We sometimes implement webview in fragment.

But 'back key' not working in webview of fragment.

Because webview is not activity but fragment.

So we add back button.

But possible to using back key in webview of fragment.

Add this code to MainActivity.java 

  1. @Override
  2. public void onBackPressed() {
  3.     if (webfragment.webview.canGoBack()) {          
  4.         try{
  5.         webfragment.webview.goBack();  //only webview back-key code
  6.         }catch(Exception e){
  7.              //back-key code of another fragment
  8.         }
  9.     }else {
  10.             // back-key code of another fragment
  11.         }

Add onBackPressed method in MainActivity.java

line 3 : Method canGoBack() means possible to go back in webview.

line 5 : Method goBack() means go back in webview.


No comments:

Post a Comment