0

When the app starts the first time, it's slow. It takes about 5 seconds staring at a white screen before the main screen shows. After that the app works fine and quick. I don't know why my app is slow at startup I try to clean the onCreate() and put most code in onStart() this my code:

public class MainActivity extends AppCompatActivity {
private WebView webView;
private  ImageView image ;
private ProgressBar loading;
private TextView textView;
private AdView mAdView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    image = (ImageView) findViewById(R.id.image);
    webView = (WebView) findViewById(R.id.webview);
    loading = (ProgressBar) findViewById(R.id.progressBar);
    textView = (TextView) findViewById(R.id.textView);
    mAdView = (AdView) findViewById(R.id.adView);




    AdRequest request = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
            .addTestDevice("00927A7823BCD302CE6574744C8B07DB")  // An example device ID
            .build();

    mAdView.loadAd(request);




    loading.setMax(100);


    //loading.setVisibility(View.VISIBLE);
}

@Override
protected void onStart() {
    super.onStart();

    if(isNetworkAvailable() == false){

        Toast.makeText(MainActivity.this,"no internet connection",Toast.LENGTH_LONG).show();

    }else {

        webView.setWebViewClient(new HelpClient());

        webView.setWebChromeClient(new WebChromeClient() {

            public void onProgressChanged(WebView view, int progress) {

                loading.setVisibility(View.VISIBLE);

                textView.setText(progress + "%");
                loading.setProgress(progress);

                if (progress == 100) {

                   // ++++++++++++++++++++++++++++++++++++++++

                    //gone the progressBar
                    loading.setVisibility(View.GONE);
                    //gone the first logo
                    image.setVisibility(View.GONE);




                    //show webview
                    webView.setVisibility(View.VISIBLE);
                }
                super.onProgressChanged(view, progress);


            }


        });


        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)

        {
            // chromium, enable hardware acceleration
            webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else

        {
            // older android version, disable hardware acceleration
            webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        webView.getSettings().setCacheMode(webSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setAppCacheEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        //webView.getSettings().setLoadWithOverviewMode(true);
        //webView.getSettings().setUseWideViewPort(true);
        //webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setSaveFormData(true);
        webSettings.setEnableSmoothTransition(true);







        webView.loadUrl("********");


        loading.setProgress(0);


    }


}




@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }
}

private class HelpClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        loading.setVisibility(View.VISIBLE);
        image.setVisibility(View.VISIBLE);
        return true;
    }
}
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

}

hackour
  • 95
  • 1
  • 11

1 Answers1

0

Actually you are loading a url in webview. So it will be always slow in your mobile application.

If you want to fast execution of your app, the only way to design your own layout and handle your data through JSON.