好的,我正在处理一个闪屏,它会暂停1.5秒,效果很好,除了一件事。一旦在onCreate中启动了timer,如果配置(方向)发生了变化,那么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);
}发布于 2011-12-01 06:01:58
您可以创建一个新的静态布尔值,将其设置为false,并在创建时仅在标志为false时执行处理程序操作...
PS:在if语句中,必须将boolean标志设置为true :)
祝好运!
发布于 2011-12-01 06:31:23
在onCreate中创建处理程序,在onDestroy中释放它,在onStart中发送消息/发布runnable,在onStop中删除message / runnable。
这将在每次旋转时重置计时器,因此,如果您每秒旋转设备,则可能会保持启动屏幕。
在Android中可能需要一秒钟左右的时间来切换旋转,你可能想要这种行为,因为它可以启动应用程序,旋转而看不到飞溅。
https://stackoverflow.com/questions/8333353
复制相似问题