Question

Ok I'm working on a splash screen that pauses for 1.5 seconds and works great, except for one thing. Once the timer is started in onCreate if the configuration (orientation) changes then the timer gets reset and then end result is it starts my ParchmentActivity.java twice.

How can I prevent the handler from sending the intent twice?

Thanks in advance!

Full code can be found @: https://github.com/n00bware/android_apps_parchment

Here is my code (from example http://www.anddev.org/novice-tutorials-f8/splash-fade-activity-animations-overridependingtransition-t9464.html):

public class SplashScreen extends Activity {

private static final int SPLASH_DISPLAY_TIME = 1500;  /* 1.5 seconds */
private static final String TAG = "Parchment";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   /* Create a new handler with which to start the main activity
      and close this splash activity after SPLASH_DISPLAY_TIME has
      elapsed. */
   new Handler().postDelayed(new Runnable() {
       @Override
       public void run() {

           Intent parchment = new Intent(SplashScreen.this, ParchmentActivity.class);
           SplashScreen.this.startActivity(parchment);
           SplashScreen.this.finish();
           overridePendingTransition(R.anim.fade_main_in, R.anim.fade_splash_out);
        }
    }, SPLASH_DISPLAY_TIME);
}

/* I found a suggestion to try overriding onConfigurationChanged()
   but it doesn't stop a new timer from being started */

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

/* I also tried overriding onStart() but this also doesn't stop a
   new timer. What exactly is called when an orientation configuration
   changes happens? */
@Override
public void onStart() {
    super.onStart();
    setContentView(R.layout.splash);
}
Was it helpful?

Solution

you can create a new static boolean, set it false and in on create do the Handler action only if the flag is false...

PS: inside the if statement, you must set the boolean flag to true :)

Good luck!

OTHER TIPS

Create the handler in onCreate, release it in onDestroy, send a message / post a runnable in onStart, remove message / runnable in onStop.

This will reset the timer with each rotate, so you could potentially keep the splash screen up if you rotated the device every second.

In Android it can take a second or so to switch rotations, you probably want this behaviour because it would be possible to start app, rotate and not see the splash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top