我正在从Intent开始一项活动。在我的代码中,我有更多的活动。但是仅在一个特定活动中,当尝试从一个Intent启动时,获得“空白白”屏幕,并且在该活动获得负载之后。所以我想到了显示ProgressBar。但是在白色屏幕之后也会显示ProgressBar。如何避免显示白屏?

从翻新请求中获取响应时,将调用getWebViewUrlSuccessResponse方法。
以下是我的第一个活动,

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


这是我的第二项活动

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


            }

        });
    }

最佳答案

白屏可能是由于布局加载延迟。

您可以检查onCreate方法中是否有长时间运行的作业,例如网络访问,数据库访问或文件访问,然后将其移至onStart方法。

onCreate应该尽快返回。

关于java - 在 Activity 之间切换时如何避免出现白屏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61938851/

10-10 01:48