Search code examples
androidandroid-webviewandroid-tabhost

WebView CanGoBack is not working inside TabHost


I have created the Dynamic Tabhost using the following link

http://www.pocketmagic.net/?p=1132

I have changed the Content with WebView as below

ts3.setContent(new TabHost.TabContentFactory(){
             public View createTabContent(String tag)
             {
                LinearLayout panel = new LinearLayout(sActiveContext);
                panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
                            LayoutParams.WRAP_CONTENT));
                panel.setOrientation(LinearLayout.VERTICAL);
                WebView webview=new WebView(sActiveContext);                
                panel.addView(webview);
                return panel;
             }
        });

webview.goback() is implemented as below

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
        {          
             if(event.getAction() == KeyEvent.ACTION_DOWN)
               {
                   switch(keyCode)
                   {
                      case KeyEvent.KEYCODE_BACK:   

                       if(webview.canGoBack())
                           webview.goBack();
                       else
                           //alert                     
                           return true;
                   }

               }
               return super.onKeyDown(keyCode, event);
        }

always i'm getting alert msg only. What i missed?

Note: Number of tabs and titles passed from client side. The above code are in server side.


Solution

  • I have solved this goback issue with WebView. I have taken the current webview from the current tab of tabhost. Now working fine

    public static WebView getCurrentWebView()
      {
         if(sTabHost!=null)
              {
          View view=sTabHost.getCurrentView();
                if(view!=null)
                {
                 View child=((ViewGroup)view).getChildAt(0);
                 for(int i=0; i<((ViewGroup)child).getChildCount(); i++) {
                   View nextChild = ((ViewGroup)child).getChildAt(i);
                   if(nextChild!=null)
                   {
                    String type=nextChild.getClass().toString();
                    if(type!=null && !type.equals("") && type.equals("class android.webkit.WebView"))
                    {
                     sCurrentWebView=(WebView) (((ViewGroup)child).getChildAt(i));                  
                     break;
                    }                 
                   }
               }
                }           
              }
         return sCurrentWebView;
      }
    
    @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) 
            {          
                 if(event.getAction() == KeyEvent.ACTION_DOWN)
                   {
                       switch(keyCode)
                       {
                          case KeyEvent.KEYCODE_BACK:   
                           WebView view=getCurrentWebView();
                           if(view!=null && view.canGoBack())
                             view.goBack();
                           else
                               //alert                     
                               return true;
                       }
    
                   }
                   return super.onKeyDown(keyCode, event);
            }