0

I am loading a web app from my android application. In the initial load of URL, it will redirect to the login page and after login, it will move to the web app home screen. But the expected behaviour is to redirect it to Native Homescreeb after login. So I have written intent to launch new activity in onPageFinished by comparing the URL. But it will show the Web page for 2 seconds before moving to Activity. I tried hiding Webview on that case. Then a blank screen is displayed for 2seconds and an Activity is displayed. See below implementation and correct me if anything is wrong.

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            try {

                String loadingURL =request.getUrl().toString();
                view.loadUrl(request.getUrl().toString());
              } catch (Exception e) {
                

            }
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            try{

                handler.proceed();

            }catch (Exception e){
                e.printStackTrace();
            }
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            if(url.contains("https://login.microsoftonline.com/"))
            {
                
                mWebview.setVisibility(View.VISIBLE);
            }
            else if(url.contains(GlobalDeclarations.BASE_URL_WEBAPP_DEV) )
            {
                progress_div.setVisibility(View.VISIBLE);

                mWebview.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPageCommitVisible(WebView view, String url) {
            super.onPageCommitVisible(view, url);
            Log.d("onPageCommitVisible", "onPageCommitVisible" + url);
        }

       @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
            
            if (mWebview.getProgress() == 100) {

                if(url.contains(GlobalDeclarations.BASE_URL_WEBAPP_DEV))
                {
                    mWebview.setVisibility(View.GONE);

                }
                else
                {
                    mWebview.setVisibility(View.VISIBLE);
                }
                progress_div.setVisibility(View.GONE);
            }
        }

        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            Log.d("onPageFinished", "onPageFinished URL........ " + url );
          
            mWebview.evaluateJavascript("common.setFromApp(" + true + ")", new ValueCallback<String>() {

                @Override
                public void onReceiveValue(String value) {
                    if (value.equals("false"))
                    {
                        mWebview.setVisibility(View.GONE);
                        Intent intent = new Intent(view.getContext(), BaseActivity.class);
                        startActivity(intent);
                    }
                    else
                    {
                        mWebview.setVisibility(View.VISIBLE);
                    }
                }
            });
Malhotra
  • 221
  • 3
  • 13

1 Answers1

0

well, thats logic behavior, you have requested for new web page, then some network I/O stuff will fetch it, after that WebView will consume returned data and draw it. and then onPageFinished gets called, in which you are calling some additional JS (which execution may also add some extra millisecs)

your only way is to make some "loading view" and showing it when WebView visibility is set to GONE, as this operation must take some time, for connection and for handling data by WebView, which is heavy and not so efficient (comparing to native views/drawing)

btw. use logcat and put some lines in called overriden methods, you will see and measure loading time

Log.i("webview_log", "methodName called at "+System.currentTimeMillis());
snachmsm
  • 17,866
  • 3
  • 32
  • 74