![](/img/trans.png)
[英]How to persist a class and data across multiple intents and orientation changes
[英]Handler().postDelayed() sending multiple intents when orientation changes
好吧,我正在制作一個暫停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);
}
您可以創建一個新的靜態布爾值,將其設置為false,並在創建時僅在標志為false時執行Handler操作...
PS:在if語句中,你必須將boolean標志設置為true :)
祝好運!
在onCreate中創建處理程序,在onDestroy中釋放它,在onStart中發送消息/發布runnable,在onStop中刪除message / runnable。
這將在每次旋轉時重置計時器,因此如果每秒旋轉一次設備,您可能會保持啟動屏幕。
在Android中,切換旋轉可能需要一秒左右,您可能需要這種行為,因為它可以啟動應用程序,旋轉而不會看到啟動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.