Search code examples
javaandroidandroid-activitysplash-screendynamic-splash-screen

Skipping a Splash Screen


I have designed a splash screen with a button. The Java code is as below. The layout of the splash contains some texts with animation and button named skipped splash screen. When the user presses the button, the splash screen has to stop immediately and open the next activity. But when I open the splash screen and press skip button, the next activity opens but after the duration for which splash screen has to run gets over, again the activity opens. How to stop the splash screen when a user presses the skip button?

  public class Qz1 extends Activity {

        TextView a;
        TextView b;
        TextView c;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_qz1);
            a =(TextView)findViewById(R.id.roundOnea22);
            a.startAnimation(AnimationUtils.loadAnimation(Qz1.this, R.anim.anim_slide_in_left));
            b =(TextView)findViewById(R.id.roundOneb);
            b.startAnimation(AnimationUtils.loadAnimation(Qz1.this, R.anim.anim_slide_in_right));
            c =(TextView)findViewById(R.id.roundme);
            c.startAnimation(AnimationUtils.loadAnimation(Qz1.this, R.anim.anim_slide_in_left));




            Thread thread = new Thread(){

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try{
                    sleep(3200);

                    startActivity(new Intent(getApplicationContext(), Qone.class));
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
        };
        thread.start();

    }

    public void round1(View v){
       Intent i = new Intent(Qz1.this, Qone.class);
       startActivity(i);
    }
}

Solution

  • Let's suppose you want to keep your first activity in the background, but you do not want the thread to re-open the second activity as soon as it has finished sleeping.

    In order to achieve that, you can make your "thread" a global variable of a custom Thread class. You can define this as an inner class of your activity:

    MyThread thread;
    

    and the class definition:

    private class MyThread extends Thread
    {
        public boolean bRun = true;
    
        @Override
        public void run()
        {
            try
            {
                sleep(3200);
                if (bRun)
                {
                    startActivity(new Intent(getApplicationContext(), Activity2.class));
                }
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    
    }
    

    In onCreate(), you write

    thread = new MyThread();
    thread.start();
    

    Then you can change your "onClick" method like this:

    public void round1(View v){
       if (thread != null && thread.isAlive())
       {
           thread.bRun = false;
       }
       Intent i = new Intent(Qz1.this, Qone.class);
       startActivity(i);
    }
    

    This will keep the thread from starting the second activity, if it has been started by clicking the button.