Create a true splash screen

前端 未结 7 573
误落风尘
误落风尘 2020-12-09 05:21

How can I make a true splash screen in Android? I don\'t want timers or delays. Just a splash screen that is shown until your application has loaded.

7条回答
  •  青春惊慌失措
    2020-12-09 06:02

    The best way to do a splash screen is:

    1. if the user press back, you need to go fast to the main screen.
    2. no animation.

    I arrived in this good solution:

    import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.WindowManager; import br.eti.fml.android.sigame.R;
    
    import java.util.concurrent.atomic.AtomicBoolean;
    
    public class LauncherActivity extends Activity {
        private AsyncTask goingToNextScreen;
        private AtomicBoolean alreadyShown = new AtomicBoolean(false);
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.launcher);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    
            //noinspection unchecked
            goingToNextScreen = new AsyncTask() {
    
                @Override
                protected Object doInBackground(Object... objects) {
                    try {
                        Thread.sleep(1500);
                    } catch (InterruptedException e) {
                        // ignores
                    }
    
                    return null;
                }
    
                @Override
                protected void onPostExecute(Object o) {
                    goNext();
                }
            }.execute();
        }
    
        @Override
        public void onBackPressed() {
            if (goingToNextScreen != null) {
                goingToNextScreen.cancel(true);
    
                goNext();
            }
        }
    
        private void goNext() {
            if (alreadyShown.compareAndSet(false, true)) {
                startActivity(new Intent(LauncherActivity.this, HomeActivity.class));
                overridePendingTransition(0, 0);
                finish();
                overridePendingTransition(0, 0);
            }
        } }
    

提交回复
热议问题