0

I have webview code in oncreate and onOptionsItemSelected which works properly as shown below.(this is my old code working as required)

MainActivity.java

 @Override
protected void onCreate(Bundle savedInstanceState) 
{       mwebView = (WebView) findViewById(R.id.pdf_web_view);
        WebSettings settings = mwebView.getSettings();
        settings.setJavaScriptEnabled(true);
        mwebView.setWebChromeClient(new WebChromeClient());
        mwebView.loadUrl("file://" + getFilesDir() + "/index.html");
}



@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {

           case R.id.action_gotopage:

            LayoutInflater layoutInflater = LayoutInflater.from(this);
            View input_dialogView = layoutInflater.inflate(
                    R.layout.gotopage_dialogbox, null);
            final AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Enter the page number");
            alert.setView(input_dialogView);

            final EditText input = (EditText) input_dialogView
                    .findViewById(R.id.pagenum_edit);

            input.requestFocus();
            input.setTextColor(Color.BLACK);

            alert.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int whichButton) {
                            String pg_num = input.getText().toString();

                            if (pg_num.equals("")) {
                                input.setError("Page number Required");

                            } else {

                                mwebView.loadUrl("javascript:onGotoPage("
                                        + pg_num + ")");

                            }
                        }

                    });

            alert.setNegativeButton("CANCEL",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int whichButton) {

                        }
                    });

            // create an alert dialog
            AlertDialog alert1 = alert.create();

            alert1.show();

            return super.onOptionsItemSelected(item);

        default:
            return super.onOptionsItemSelected(item);
        }
      }

I want to convert this code to slide pages using ViewPager. So i have the code like this(My new code) :

MainActivity.java

public class Mainactivity extends AppCompatActivity {
    private  int NUM_PAGES=10;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pdf_webview);

    mPager = (ViewPager) findViewById(R.id.pdf_view_pager);
    mPagerAdapter = new 
    ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
   }

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return new SlideFragment().create(position);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
  }
}

SlideFragment.java

 public class SlideFragment extends Fragment {
    private static final String ARG_PAGE = "page";
    private int mPageNumber;
    private WebView mwebView;

    public static SlideFragment create(int pageNumber) {
        SlideFragment fragment = new SlideFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, pageNumber);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPageNumber = getArguments().getInt(ARG_PAGE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.pdf_fragment_webview, container, false);

            mwebView = (WebView) findViewById(R.id.pdf_web_view);
            WebSettings settings = mwebView.getSettings();
            settings.setJavaScriptEnabled(true);
            mwebView.setWebChromeClient(new WebChromeClient());
            mwebView.loadUrl("file://" + getFilesDir() + "/index.html");
            mwebView.loadUrl("javascript:onGotoPage("
                                            + pg_num + ")");

      }
     }

The webview.loadURL only works first time, it doesn't work second time. But when it is inside onOptionsItemSelected method, it works fine.

I have tried all the solutions from WebView loadUrl works only once

Also tried onPageFinished and evaluate function.

But none worked in my case.I am not able to figure out what is wrong.

3
  • To clarify - when you say that if doesn't work the second time, you mean that mwebView.loadUrl("javascript:onGotoPage(" + pg_num + ")"); is not having any effect? May 16, 2017 at 5:24
  • @SimplyJaymin Yes correct. It does not have any effect. May 16, 2017 at 6:34
  • @SimplyJaymin yes , you are correct. It is not having any effect.mwebView.loadUrl("javascript:onGotoPage(" + pg_num + ")"); this works in previous case though. May 16, 2017 at 6:36

1 Answer 1

0

I think there is a race condition, between the html page loading and the javascript function running.

Would suggest you first load the html page, then in the onPageFinished callback run the javascript.

Also, when running javascript code on webview, you should use evaluateJavascript method.

Hope this helps.

1
  • @sandeshhegde Thanks for sharing your code. I do not see onPageFinished callback. I think you need to run the javascript code only after the html page has finished loading. When you load a html page, it takes some time for it to complete loading, and only after it has loading should you execute any javascript code. Follow the steps as listed in my answer. May 26, 2017 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.