简体   繁体   中英

How to avoid White screen when switching between Activities

I am starting an Activity from Intent. In my code I have more number of Activities. But only in one Specific Activity, when trying to launch from an Intent, getting Blank White screen and after that Activity is getting load. So I thought of displaying ProgressBar. But ProgressBar also displayed after White screen. How to avoid displaying White screen?Thanks in advance.

getWebViewUrlSuccessResponse method is called when getting response from Retrofit request. Below is my first activity,

@Override
    public void getWebViewUrlSuccessResponse(String url,String id,String currency) {

        Intent intent=new Intent(FirstActivity.this, WebViewActivity.class);
        intent.putExtra("url",url);
        intent.putExtra("id",id);
        intent.putExtra("name",name);
        intent.putExtra("password",password);
        intent.putExtra("code",code);
        intent.putExtra("currency",currency);
        startActivity(intent);
    }

This is my second activity

 protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidLogger.log(5,TAG,"WEBVIEW");
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_webview);
        AndroidLogger.log(5,TAG,"progress bar made visible");
       Intent intent=getIntent();
        RelativeLayout layout = findViewById(R.id.payment_gateway_relative_layout);
        ProgressBar progressBar = new ProgressBar(PaymentGatewayActivity.this,null,android.R.attr.progressBarStyleSmallInverse);
        progressBar.setIndeterminate(true);
        progressBar.setVisibility(View.VISIBLE);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        layout.addView(progressBar,params);

        //setContentView(layout);
       this.name=intent.getStringExtra("name");
       this.url=intent.getStringExtra("url");
       this.id=intent.getStringExtra("id");
       this.code=intent.getStringExtra("code");
       this.password=intent.getStringExtra("password");
       this.currency=intent.getStringExtra("currency");
        webView =(WebView)findViewById(R.id.webView);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
        webView.loadUrl(url);

        webView.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

                // do your handling codes here, which url is the requested url
                // probably you need to open that url rather than redirect:
                AndroidLogger.log(5, TAG, "url:" + url);
                AndroidLogger.log(5, TAG, "view.geturl" + view.getUrl());
               //AndroidLogger.log(5,TAG,view.goBack()); view.canGoBack();

               view.loadUrl(url);

                return false; // then it is not handled by default action
            }

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

                progressBar.setVisibility(View.VISIBLE);
                AndroidLogger.log(5,TAG,"started"+url);

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                AndroidLogger.log(5,TAG,"Finished"+url);

                webView.setVisibility(View.VISIBLE);


            }

        });
    }

The white screen is probably due to latency in your layout loading.

You may check your onCreate method for long running jobs like Network access, database access or file access, and move them to the onStart method.

onCreate should return as soon as possible.

You can possibly try to change default activity background in your theme <item name="android:windowBackground"> . But anyway after that you'll get that background color or image until your activity measure and draw your layout. So here is code snippet in my styles.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/background_color</item>
</style>

Change background color and you get it

line webView.loadUrl(url); causes White Screen..Because in my code I am using postUrl to load webview which I didnt mention in code. Two times I am trying to load URL. That is the problem. After removing that line everything works fine. Thank you all who tried to help me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM