I have written this 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);
}
}
My question is: since the run
callback is executed after the time I've indicated (according to this doc: https://developer.android.com/reference/android/os/Handler) , should I replace its content with the following code (assuming that
being an 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();
}
Note that Android Studio says that == null
is always false and should be removed.
Use isDestroyed() || isFinishing()
or just call removeCallbacksAndMessages
to remove any pending posts of callbacks:
@Override
protected void onDestroy() {
if (the_transition_handler != null) {
the_transition_handler.removeCallbacksAndMessages(null);
}
super.onDestroy();
}