1

I have dynamically created the action bar and tabs. I have define a class for tab fragments like the below code.

public static class TabFragmentClass extends Fragment
{   
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
       try
       {
           linearLayout=new LinearLayout(sActiveContext);
           linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
           linearLayout.setOrientation(LinearLayout.VERTICAL);
           CustomWebView webview=new CustomWebView(sActiveContext);
           FrameLayout layout=webview.createwebview();
           for (int i = 0; i < arrayList.size(); i++) {
               if(sActionBar.getSelectedTab().getPosition()==i)
               {
                   webview.initwebview(arrayList.get(i));
                   mWebViewList.add(i, webview);
                   break;
               }
           }
           linearLayout.addView(layout);
           linearLayout.setId(sActionBar.getSelectedTab().getPosition());
           return linearLayout;
        }
        catch(Exception error)
        {
            System.out.println(error.getMessage());
            return null;
        }

 }
   }

The url is the local html file having their own native java method calls. If I am select the action bar tab first time, this is working fine. That is native java method is correctly called and callback is success. If I am visiting the tab second time, the fragment is only shown, tabs content is not re-created. I need the functionality of tab content not created each time. But I am facing the issue of native method not defined error. That is the native method is not called at all. How can I fix the issue?

Karthick
  • 1,241
  • 5
  • 20
  • 43

2 Answers2

1

Change your fragment to this,

public static class TabFragmentClass extends Fragment
{   
   private static CustomWebView webview=null;
   private static boolean isInitialized=false;
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
       try
       {
           linearLayout=new LinearLayout(sActiveContext);
           linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
           linearLayout.setOrientation(LinearLayout.VERTICAL);
           webview=new CustomWebView(sActiveContext);
           FrameLayout layout=webview.createwebview();
           onLoadWebview();
           linearLayout.addView(layout);
           linearLayout.setId(sActionBar.getSelectedTab().getPosition());
           return linearLayout;
        }
        catch(Exception error)
        {
            System.out.println(error.getMessage());
            return null;
        }

 }
@Override
public void onActivityCreated()
{
super.onActivityCreated();
isInitialized=true;
}
@Override
public void onResume()
{
if(isInitialized)
    onLoadWebView();
}
public void onLoadWebview()
{
for (int i = 0; i < arrayList.size(); i++) {
               if(sActionBar.getSelectedTab().getPosition()==i)
               {
                   webview.initwebview(arrayList.get(i));
                   mWebViewList.add(i, webview);
                   break;
               }
           }
}
   }
Sino Raj
  • 6,431
  • 2
  • 22
  • 23
  • Hi Sino, the issue is not fixed. Still the last visited url is loaded into the current webview. Please give suggestions. – Karthick Apr 18 '13 at 15:08
  • Are you check that, the method is calls onResume()? – Sino Raj Apr 19 '13 at 04:01
  • Yes checked. The resume method is called. The fragment shows the correct webview with correct url. But if I am calling to the native methods means, getting the current webview as we discussed before. I tried webview.getUrl() method, that time it shows only the last visited url.Obviously the native callback have no method. So, the values from native method is not displayed. "The method is undefined " error I got in the logcat. – Karthick Apr 19 '13 at 04:08
  • Hi Sino,Any suggestion about the issue. – Karthick Apr 19 '13 at 05:19
  • If the fragment is recreated the webview is loaded with specific url corresponding to the tab. If I am showing the fragment, the lastly visited url is in the currentwebview. I have tested this using getCurrentWebview() method and call getUrl(). But I need the functionality of the fragment not re-created every time. But the javascript function is not returned back to the specific url instead this will search the callback method in the lastly visited url. How can I resolve this.? Please help me Sino. – Karthick Apr 19 '13 at 06:57
0

onCreateView only will be create Once ONLY after Activity Created You can put your method in onResume() or refresh web You can see the Fragment's life circle here

buptcoder
  • 2,692
  • 19
  • 22
  • Thanks for the response. If I am place the methods which are in the onCreateView() to the onResume() method, switching between the tabs re-created the content. How can I restrict this.? – Karthick Apr 18 '13 at 08:38
  • The URL is changed in the webview. Last visited url is loaded into the current webview. This is the issue. Could you please help me.? – Karthick Apr 18 '13 at 12:22
  • I think you can try webview.reload() on Resume. It will reload html and result in the native method called. – buptcoder Apr 19 '13 at 00:54
  • If I am re-loading the webview, the tab content is re-created every time. – Karthick Apr 19 '13 at 03:35
  • The reloading of the webview is not fixed the issue. still the last visited url is loaded. But the fragment shows the correct webview. – Karthick Apr 19 '13 at 03:51
  • Does arraylist include the last URI? And What codes of initwebview? – buptcoder Apr 19 '13 at 04:07
  • arraylist include all the urls.In the initwebview() method, I just define the webview settings javascript interface finally load the url. Nothing doing anything inside the initwebview() – Karthick Apr 19 '13 at 04:33