0

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);
}
César
  • 9,939
  • 6
  • 53
  • 74
JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
  • CommonsWare suggested I check if 'savedInstanceState' was null then start then setup the intent, because if it get returned null then it is first run but is !null when the orientation changes and this method is simple and works very well -here is the link http://groups.google.com/group/android-developers/msg/9dcf073cfe5c863e – JBirdVegas Dec 01 '11 at 20:26

2 Answers2

1

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!

Cata
  • 11,133
  • 11
  • 65
  • 86
  • a wonderfully simple approach however this will work once; I set it above the class declaration to false used if (!splash_once) and it worked perfectly once (yes I set it to true within the if()). From then on every run was stuck on the splash and never ran the intent – JBirdVegas Dec 01 '11 at 01:02
  • you can set the flag to false again in onDestroy() method :) – Cata Dec 01 '11 at 10:01
  • I'm going to mark this as correct as I'm doing something very similar but instead setting the boolean to read if savedInstanceState is null Thanks for the help! – JBirdVegas Dec 01 '11 at 20:33
1

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.

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
  • I was hoping to have the timer absolute and unaffected by orientation configuration changes or any user interaction ... if put against a wall I would be ok with extending the timer but I'm not doing any background processes so I feel it would be unnecessarily cumbersome to the user – JBirdVegas Dec 01 '11 at 01:07
  • You could return the time you should keep the splash screen up from [onRetainNonConfigurationInstance](http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance()) and get it from [getLastNonConfigurationInstance](http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance()) – FunkTheMonk Dec 01 '11 at 11:13