Skipping a Splash Screen

2k Views Asked by At

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);
    }
}
5

There are 5 best solutions below

20
Bö macht Blau On BEST ANSWER

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.

2
Asad Mehmood On

It's best practice to use Async Tasks for wait/sleep scenarios, such as for splash screens, but requirements can differ.

Anyway this is my way to call a splash screen:

Create the AsyncTask first.

private class SplashTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        finish();

            Intent intent = new Intent(SplashActivity.this,
                    MainActivity.class);
            startActivity(intent);

        }
    }
}

Then call this where ever you want: on button click, on start, or on create:

new SplashTask().execute();
0
Hughzi On

Shouldn't be using sleep(2000)

use an animationlistener (http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html)

when onAnimationEnd is triggered call startActivity.

1
Nabeel K On

try this in the splash activity

Button button = (Button)findViewById(R.id.buttonLayout);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(this,TargetActivity.class));
                finish();
            }
        });
0
z3n105 On

I think best practice here would be to use a Handler.

You can do it like this:

public class Test extends AppCompatActivity{

private Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Settign the splashscreen with the button i suppose
    setContentView(R.id.splashcreen);
}

@Override
protected void onStart() {
    super.onStart();

    handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            startNextActivity();
        }
    }, 2000);
}

public void startNextActivity(){
    startActivity(new Intent(getApplicationContext(), Qone.class));
}

public void skipSplashScreen(){
    if (handler != null)
        handler.removeCallbacksAndMessages(null);

    startNextActivity();
}

@Override
protected void onStop() {
    super.onStop();
    // clear handler on stop
    if (handler != null)
        handler.removeCallbacksAndMessages(null);
}
}

CAll skipSplashScreen() when user press the button and the handler will stop so the timer stops and you go to next activity manually by calling method startNextActivity().