Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

android - Fragment webview java script function is not working

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
               }
           }
}
   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...