Handler().postDelayed() 在方向改变时发送多个意图
好吧,我正在开发一个启动屏幕,它会暂停 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);
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个新的静态布尔值,将其设置为 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!
在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.