Handler 的 postDelayed 回调:检查 FragmentActivity 是否不为 null 且未被销毁?
Handler's postDelayed's callback: Checking if the FragmentActivity is not null and not destroyed?
我写了这个class:
public class SplashScreen extends AppCompatActivity {
private Handler the_transition_handler;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
}
@Override
protected void onStart() {
super.onStart();
startTheTransitionAfterTheSplashScreen();
}
@Override
protected void onDestroy() {
super.onDestroy();
the_transition_handler.removeCallbacksAndMessages(null);
}
private void startTheTransitionAfterTheSplashScreen() {
the_transition_handler = new Handler();
the_transition_handler.postDelayed(new Runnable() {
@Override
public void run() {
final Intent intentSplashScreenToActivityJustAfterSplashScreen = new Intent(SplashScreen.this, ActivityJustAfterSplashScreen.class);
startActivity(intentSplashScreenToActivityJustAfterSplashScreen);
overridePendingTransition(R.anim.animation_enter_activity, R.anim.animation_leave_activity);
finish();
}
}, 1000);
}
}
我的问题是:由于 run
回调是在我指定的时间之后执行的(根据此文档:https://developer.android.com/reference/android/os/Handler),我是否应该将其内容替换为以下代码(假设that
是 AppCompatActivity
)?
@Override
public void run() {
if(that == null || that.isDestroyed()) {
return;
}
final Intent intentSplashScreenToActivityJustAfterSplashScreen = new Intent(SplashScreen.this, ActivityJustAfterSplashScreen.class);
startActivity(intentSplashScreenToActivityJustAfterSplashScreen);
overridePendingTransition(R.anim.animation_enter_activity, R.anim.animation_leave_activity);
finish();
}
请注意,Android Studio 表示 that == null
始终为错误,应将其删除。
使用 isDestroyed() || isFinishing()
或仅调用 removeCallbacksAndMessages
删除任何待处理的回调帖子:
@Override
protected void onDestroy() {
if (the_transition_handler != null) {
the_transition_handler.removeCallbacksAndMessages(null);
}
super.onDestroy();
}
我写了这个class:
public class SplashScreen extends AppCompatActivity {
private Handler the_transition_handler;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
}
@Override
protected void onStart() {
super.onStart();
startTheTransitionAfterTheSplashScreen();
}
@Override
protected void onDestroy() {
super.onDestroy();
the_transition_handler.removeCallbacksAndMessages(null);
}
private void startTheTransitionAfterTheSplashScreen() {
the_transition_handler = new Handler();
the_transition_handler.postDelayed(new Runnable() {
@Override
public void run() {
final Intent intentSplashScreenToActivityJustAfterSplashScreen = new Intent(SplashScreen.this, ActivityJustAfterSplashScreen.class);
startActivity(intentSplashScreenToActivityJustAfterSplashScreen);
overridePendingTransition(R.anim.animation_enter_activity, R.anim.animation_leave_activity);
finish();
}
}, 1000);
}
}
我的问题是:由于 run
回调是在我指定的时间之后执行的(根据此文档:https://developer.android.com/reference/android/os/Handler),我是否应该将其内容替换为以下代码(假设that
是 AppCompatActivity
)?
@Override
public void run() {
if(that == null || that.isDestroyed()) {
return;
}
final Intent intentSplashScreenToActivityJustAfterSplashScreen = new Intent(SplashScreen.this, ActivityJustAfterSplashScreen.class);
startActivity(intentSplashScreenToActivityJustAfterSplashScreen);
overridePendingTransition(R.anim.animation_enter_activity, R.anim.animation_leave_activity);
finish();
}
请注意,Android Studio 表示 that == null
始终为错误,应将其删除。
使用 isDestroyed() || isFinishing()
或仅调用 removeCallbacksAndMessages
删除任何待处理的回调帖子:
@Override
protected void onDestroy() {
if (the_transition_handler != null) {
the_transition_handler.removeCallbacksAndMessages(null);
}
super.onDestroy();
}