Activity 首次启动时首次加载时完成速度太快

Activity finishes too quickly when loading for the first time at first launch

我仅在应用程序首次启动时使用此代码进行 Activity 加载,但出现问题,应该首次启动的 ActivityWebView,而 WebView 加载时 Activity 完成得太快。这是我第一次使用 Activity 加载的代码。

count = readSharedPreferenceInt("cntSP","cntKey");
if(count==0){
    Intent intent = new Intent();
    intent.setClass(MainActivity.this, TemporaryActivity.class);
    startActivity(intent);
    count++;
    writeSharedPreference(count,"cntSP","cntKey");
}

//Read from Shared Preferance
public int readSharedPreferenceInt(String spName,String key){
    SharedPreferences sharedPreferences = getSharedPreferences(spName,Context.MODE_PRIVATE);
    return tempInt = sharedPreferences.getInt(key, 0);
}     

//write shared preferences in integer
public void writeSharedPreference(int ammount,String spName,String key ){
    SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putInt(key, ammount);
    editor.commit();
}

这是第一次加载activity的代码

public class Reg_Status extends AppCompatActivity {
 Context a;
 private ProgressBar pBar;

/* renamed from: com.partners.app.Confmap.1 */
class C01561 extends WebViewClient {
    C01561() {
    }

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        Reg_Status.this.pBar.setVisibility(ProgressBar.VISIBLE);
    }

    public void onPageFinished(WebView view, String url) {
        Reg_Status.this.pBar.setVisibility(ProgressBar.INVISIBLE);
    }

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        view.loadUrl(Config.URL_INTERNET_ERROR);
        Reg_Status.this.pBar.setVisibility(ProgressBar.VISIBLE);
    }
}



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView((int) R.layout.activity_reg_status);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    this.pBar = (ProgressBar) findViewById(R.id.pBar);
    WebView webView = (WebView) findViewById(R.id.wView_reg);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new C01561());
    webView.loadUrl(Config.URL_REG);


}

创建一个处理程序以延迟第二个处理程序的启动Activity。

new Handler().postDelayed(new Runnable() {


        @Override
        public void run() {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, TemporaryActivity.class);
            startActivity(intent);

        }
    }, TIME_OUT);

现在,在您的 if 条件中调用此处理程序并相应地设置 TIME_OUT

好的。您已实现此方法 onPageFinished(),并在该方法内 onCreate() 内执行您正在执行的片段。

public void onPageFinished(WebView view, String url) {
    //You code, which you had put inside onCreate() should go here.
}

编辑

Intent intent = new Intent(MainActivity.this, TemporaryActivity.class);
startActivity(intent);
...

新编辑

public class C01561 extends WebViewClient {
    private Context context;
    C01561(Context context) {
        this.context = context;
    }

    public void onPageFinished(WebView view, String url) {
        Reg_Status.this.pBar.setVisibility(ProgressBar.INVISIBLE);

        Intent intent = new Intent(context, TemporaryActivity.class);
        startActivity(intent);
    }
}

并且当您创建 WebViewClient 的实例时,请确保传递 MainActivity 上下文。

webView.setWebViewClient(new C01561(MainActivity.this));