0
votes

I've used a splash screen and progress bar in my Android App.But after splash disappears,then next screen becomes black before switching to my main activity. But I don't want black screen.Can anyone please explain what's going on here and how can I prevent that black screen ? This is my Splash Java class.

public class Splash extends AppCompatActivity {
    private ProgressBar mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);

        new Thread((new Runnable() {
            @Override
            public void run() {
                doWork();
                startApp();
                finish();
            }
        }
        )).start();
    }

    private void doWork() {
        for (int progress = 0; progress < 100; progress += 10) {
            try {
                Thread.sleep(5500);
                mProgress.setProgress(progress);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void startApp() {
        Intent intent = new Intent(Splash.this, MainActivity.class);
        startActivity(intent);

    }
}
4
What exactly you want? Do you required next screen when progress finishes? or you want splash screen to show for few seconds and shift to next screen?aman arora

4 Answers

0
votes

I think you could remove the finish() inside the Thread.

0
votes
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);

    new Thread((new Runnable() {
        @Override
        public void run() {
            doWork();
            startApp();

        }
    }
    )).start();
}

private void doWork() {
    for (int progress = 0; progress < 100; progress += 10) {
        try {
            Thread.sleep(5500);
            mProgress.setProgress(progress);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

private void startApp() {
    Intent intent = new Intent(Splash.this, MainActivity.class);
    startActivity(intent);

}

}

0
votes

Approach 1: If you required splash for few seconds:

new Thread((new Runnable() {
        @Override
        public void run() {
            try { Thread.sleep(5500); }catch(Exception e) {}
            startApp();
            finish();
        }
    }
    )).start();

Approach 2: Show next screen when splash finish:

 public class Splash extends AppCompatActivity {
    private ProgressBar mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);

        new Thread((new Runnable() {
            @Override
            public void run() {
                doWork();

            }
        }
        )).start();
    }

    private void doWork() {
        for (int progress = 0; progress < 100; progress += 10) {
            try {
                Thread.sleep(5500);
                mProgress.setProgress(progress);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        startApp();
        finish();
    }

    private void startApp() {
        Intent intent = new Intent(Splash.this, MainActivity.class);
        startActivity(intent);

    } 
}
0
votes

Try this one:

public class Splash extends AppCompatActivity {
    private ProgressBar mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);

        new Thread((new Runnable() {
            @Override
            public void run() {
                doWork();
                }
        }
        )).start();
    }

    private void doWork() {
        for (int progress = 0; progress < 100; progress += 10) {
            try {
                Thread.sleep(5500);
                mProgress.setProgress(progress);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
          startApp();
    }

    private void startApp() {
        Intent intent = new Intent(Splash.this, MainActivity.class);
        startActivity(intent);
     finish();
    }
}