添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
强悍的太阳  ·  AppPlatCore/Code-FineU ...·  2 周前    · 
腹黑的铅笔  ·  ASP.NET Core Blazor ...·  1 周前    · 
不羁的胡萝卜  ·  NoSuchMethodError: ...·  2 周前    · 
幸福的帽子  ·  Add support for merge ...·  2 月前    · 
高大的大象  ·  Style Function not ...·  7 月前    · 
冲动的树叶  ·  SpotBugs - ...·  8 月前    · 

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange

Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up.

Sign up to join this community

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Starting an activity after 1000ms from a splash screen with Runnable and Handler().postDelayed

Ask Question

I have implemented a splash screen which is the main activity of my Android app. This splash screen shows an image, and after 1000ms there is an animation to show another activity's layout.

The code I've written is the following:

public class SplashScreen extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        startTheTransitionAfterTheSplashScreen();
    private void startTheTransitionAfterTheSplashScreen() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intentSplashScreenToActivityJustAfterSplashScreen = new Intent(SplashScreen.this, ActivityJustAfterSplashScreen.class);
                startActivity(intentSplashScreenToActivityJustAfterSplashScreen);
                overridePendingTransition(R.anim.animation_enter_activity, R.anim.animation_leave_activity);
                finish();
        }, 1000);

And for the animations:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="500" />
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%" android:toXDelta="-100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="500" />

Question

What I would want to ask is especially about the startTheTransitionAfterTheSplashScreen (I think the animations are correctly written): should I actually use a Runnable with a Handler to delay the transition? (in other words: is it suitable for this splash screen, or is there a better way to do that?)

Implementing K.H.'s answer:

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);
    \$\begingroup\$

I don't like name startTheTransitionAfterTheSplashScreen. Too long, you are already in SplashScreen. Maybe startTransition or startTransitionafter1second.

Using handler is fine, but what if user manages to change orientation during that 1 second? Activity gets destroyed and recreated. Now you have 2 active handlers and I expect ActivityJustAfterSplashScreen will be called 2 times.

I suggest keeping reference to handler so that you can cancel in onDestroy - handler.removeCallbacksAndMessages(null).

\$\begingroup\$ Thank you! I have edited my code to show you the modification. Do you agree with them? \$\endgroup\$ – JarsOfJam-Scheduler Dec 28, 2019 at 14:17 \$\begingroup\$ Yep, just use camelcase for class property the_transition_handler. \$\endgroup\$ – K.H. Dec 28, 2019 at 14:25

Thanks for contributing an answer to Code Review Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.