Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android cancelable splash screen

I'm trying to create a Splash page that has a banner and cancel button, by the following rules:

  1. if nothing is pressed, go to main activity after 5 seconds.
  2. if cancel button is pressed, go to main activity immediately.
  3. if banner is pressed, go to other activity.

My code:

public class BannerSplashActivity extends Activity {
    private static final int TIMEOUT = 5000;

    private View mButtonCancel;
    private SplashHolderTask mSplashHolderTask;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form_banner_splash);

        // Excecute task
        mSplashHolderTask = new SplashHolderTask();
        mSplashHolderTask.execute();

        // find references
        mButtonCancel = findViewById(R.id.buttonCancel);
        mButtonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSplashHolderTask.cancel(true);
                goToMainActivity();
            }
        });
    }

    private void goToMainActivity(){
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }


    /**
     * This task holds the bannerSplash
     */
    private class SplashHolderTask extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... params) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    goToMainActivity();
                }
            }, TIMEOUT);

            return null;
        }
    }
}

When I run my code I'm getting:

Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

How can I implement that logic?

like image 234
NickF Avatar asked Nov 17 '25 22:11

NickF


1 Answers

You can't create a Thread in an AsyncTask. You can simply use a simple delayed handler that you will cancel in case of a click on the banner, the cancel button or if the user leave the app :

public class BannerSplashActivity extends Activity {
    private static final int TIMEOUT = 5000;

    private Handler cancelHandler;

    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.form_banner_splash);

      cancelHandler = new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
             goToMainActivity();
        }
      };
      cancelHandler.sendEmptyMessageDelayed(0, TIMEOUT);

      // find references
      View mButtonCancel = findViewById(R.id.buttonCancel);
      View mBannerView = findViewById(R.id.bannerView);

      mButtonCancel.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              cancelHandler.removeMessages(0);
              goToMainActivity();
          }
      });

      mBannerView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              cancelHandler.removeMessages(0);
              //go to the activity you want
          }
      });
 }

  private void goToMainActivity(){
      startActivity(new Intent(this, MainActivity.class));
      finish();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if(cancelHandler != null){
      cancelHandler.removeMessages(0);
    }
  }
}    
like image 173
LucasFM Avatar answered Nov 21 '25 08:11

LucasFM