4

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);
    }
}
Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46

5 Answers5

2

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.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • thread.bRun = false; This is line is shown as error. The correction suggested is to rename the file. What to do? – Suresh Anand Nov 18 '15 at 13:11
  • @Suresh Anand - "File"? that's strange. OK, then make your IDE happy and name the variable something else, not "thread" – Bö macht Blau Nov 18 '15 at 13:13
  • Yeah did that. Now the error is bRun cannot be resolved into a field or is not a field. – Suresh Anand Nov 18 '15 at 13:17
  • but you did copy my version of your thread with the public boolean variable named "bRun"? – Bö macht Blau Nov 18 '15 at 13:18
  • @Suresh Anand - ok, I'm working around that - please wait a minute. I think one should use an inner class for the thread. Sorry I did not take the time to test this first :( – Bö macht Blau Nov 18 '15 at 13:22
  • @SureshAnand - I introduced an inner class extending Thread. This way, the boolean is "visible" in the onClickListener. The code in "onCreate()" is reduced to two lines, the rest stays like before. – Bö macht Blau Nov 18 '15 at 13:29
  • Still the same error persists. Cannot be resolved into field. – Suresh Anand Nov 18 '15 at 13:33
  • @Suresh Anand - ok, but meanwhile I have your sample running ( and working, finally ;) ) in my emulator. – Bö macht Blau Nov 18 '15 at 13:35
  • @Suresh Anand - so let's check this together. 1) You need a global variable of class "MyThread". 2) you need the inner class called MyThread which extends Thread. OK so far? – Bö macht Blau Nov 18 '15 at 13:38
  • What can be the possible solution? – Suresh Anand Nov 18 '15 at 13:41
  • @Suresh Anand - it's working for my emulator, we'll get it to work for yours :) - where did you copy the code for the MyThread class? Can the IDE find it? – Bö macht Blau Nov 18 '15 at 13:42
  • I am trying to put the code here but the site says the message is too long. Can you start a chat with me? – Suresh Anand Nov 18 '15 at 13:45
  • @Suresh Anand - maybe we'll get the chat started automatically if we write lots of comments – Bö macht Blau Nov 18 '15 at 13:46
  • I will put my code in 2 or 3 comments public class Qz1 extends Activity { Thread thread; TextView a; Button p; TextView b; TextView c; private class MyThread extends Thread { public boolean bRun = true; @Override public void run() { try { sleep(3200); if (bRun) { startActivity(new Intent(getApplicationContext(), Qone.class)); } } catch (InterruptedException e) { e.printStackTrace(); } } } – Suresh Anand Nov 18 '15 at 13:49
  • @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_qz1); thread = new MyThread(); thread.start(); } public void round1(View v){ if (thread != null && thread.isAlive()) { thread.bRun = false; } Intent i = new Intent(Qz1.this, Qone.class); startActivity(i); } } – Suresh Anand Nov 18 '15 at 13:50
  • @Suresh Anand - OK, I see where the problem is: you still write "Thread thread:". But it has to be "MyThread thread;" now – Bö macht Blau Nov 18 '15 at 13:51
  • I have added animation for activity transition. When I start the activity by clicking the button, I am getting the animation. But when the activity starts itself after the splash screen is over, the animation is not coming. How to solve that? – Suresh Anand Nov 18 '15 at 14:53
  • @Suresh Anand - we finälly have a chat :) please post your code there. I'm a little busy now, but I'll look into it in the next 24 hours. Alternatively maybe you could ask a new question. – Bö macht Blau Nov 18 '15 at 15:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95453/discussion-between-0x0nosugar-and-suresh-anand). – Bö macht Blau Nov 18 '15 at 15:16
  • yes this is good approach only that you can have `boolean` as global variable and buddy at least accept the answer if your problem is solved so this will help other too with same problem.. – Iamat8 Nov 18 '15 at 17:10
1

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();
trincot
  • 317,000
  • 35
  • 244
  • 286
Asad Mehmood
  • 315
  • 1
  • 3
  • 20
1

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.

Hughzi
  • 2,470
  • 1
  • 20
  • 30
1

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().

z3n105
  • 1,935
  • 1
  • 15
  • 14
0

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();
            }
        });
Nabeel K
  • 5,938
  • 11
  • 38
  • 68