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

发布于 2024-12-18 15:30:08 字数 2037 浏览 3 评论 0原文

好吧,我正在开发一个启动屏幕,它会暂停 1.5 秒,并且效果很好,除了一件事之外。一旦 timeronCreate 中启动,如果配置(方向)发生变化,那么 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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年不长 2024-12-25 15:30:08

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

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

祝你好运!

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!

暮凉 2024-12-25 15:30:08

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

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

在 Android 中,可能需要一秒钟左右的时间来切换旋转,您可能需要这种行为,因为可能会启动应用程序、旋转而不会看到启动画面。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文