首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Handler().postDelayed()在方向改变时发送多个意图

Handler().postDelayed()在方向改变时发送多个意图
EN

Stack Overflow用户
提问于 2011-12-01 05:44:40
回答 2查看 3.5K关注 0票数 0

好的,我正在处理一个闪屏,它会暂停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):

代码语言:javascript
复制
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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-01 06:01:58

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

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

祝好运!

票数 1
EN

Stack Overflow用户

发布于 2011-12-01 06:31:23

在onCreate中创建处理程序,在onDestroy中释放它,在onStart中发送消息/发布runnable,在onStop中删除message / runnable。

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8333353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档