Search code examples
javaandroidsplash-screen

Keep a screen active for x seconds


I am working with a splash screen but am doing a network call on the splash screen so the data is ready on the home screen by the time the user gets there.

Once the network call is complete it goes to the next screen, I would like the splash screen to be open though for at least x seconds even if the network call took a couple ms because when the network call is ms then the splash screen cannot even be seen.

I know how to do the delay the splash screen for x seconds but would like the network call to be included in those x seconds as opposed to network call time + x amount seconds.

Thank you


Solution

  • Use Handler in side onPostExecute() Method. When your Network call in complete, call handler, it'll wait for x seconds and then after moves to another activity.

    getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
    
            @Override
            public void run() {
                startActivity(new Intent(this, NextActivity.class));
            }
        }, x);// here x is time in millisecond 1 second = 1000 millseconds
    
    } });
    

    This will surely help you.