0

好的,我正在制作一个暂停 1.5 秒并且效果很好的启动画面,除了一件事。一旦timer启动,onCreate如果配置(方向)发生变化,则timer重置,然后最终结果是它启动了我的ParchmentActivity.java两次。

如何防止处理程序两次发送意图?

提前致谢!

完整代码可见@:https ://github.com/n00bware/android_apps_parchment

这是我的代码(来自示例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);
}
4

2 回答 2

1

您可以创建一个新的静态布尔值,将其设置为 false 并在创建时仅在标志为 false 时执行 Handler 操作...

PS:在 if 语句中,您必须将布尔标志设置为 true :)

祝你好运!

于 2011-11-30T22:01:57.840 回答
1

在 onCreate 中创建处理程序,在 onDestroy 中释放它,在 onStart 中发送消息/发布可运行对象,在 onStop 中删除消息/可运行对象。

这将在每次旋转时重置计时器,因此如果您每秒旋转一次设备,您可能会保持启动画面。

在 Android 中,切换旋转可能需要一秒钟左右的时间,您可能想要这种行为,因为它可以启动应用程序,旋转并且看不到飞溅。

于 2011-11-30T22:31:22.730 回答