Search code examples
androidandroid-layoutandroid-intentandroid-touch-event

How to handle automatic screen switching and also touch event to move to another screen android?


I am doing a splash screen in which after certain duration it switch over to another screen. If user doesn't want to wait for that duration, they can tap on the splash screen to switch over to another screen. But problem is, when user tap on the splash screen, it goes to another screen. Simlutaneously automatic for certain duration also runs so that, two times second screen opens. How to overcome this issue?

Below is the code which I tried where I need to handle tap and auto switching to another screen but it fails.

 public class SplashScreen extends AppCompatActivity {
        private TextView bride, groom, weds;
        private TextView date;
        private LinearLayout animLayout;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    //Remove notification bar
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

            setContentView(R.layout.activity_splash);

         new Handler().postDelayed(new Runnable() {
    // when user taps and move to next screen, after certain duration, this also calls where two times WelcomePage is called.

                @Override
                public void run() {
                    // This method will be executed once the timer is over
                    // Start your app main activity
                    Intent i = new Intent(SplashScreen.this, WelcomePage.class);
                    startActivity(i);
                    //overridePendingTransition(R.drawable.from_middle, R.drawable.to_middle);
                    overridePendingTransition(R.anim.left_right_anim, R.anim.translate);
                    // close this activity
                    finish();
                }
            }, 5000);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            int X = (int) event.getX();
            int Y = (int) event.getY();
            int eventaction = event.getAction();

            switch (eventaction) {
                case MotionEvent.ACTION_DOWN:
  //i need to handle this case too..when user taps the screen to move.


  Intent i = new Intent(SplashScreen.this, WelcomePage.class);
                startActivity(i);
                //overridePendingTransition(R.drawable.from_middle, R.drawable.to_middle);
                overridePendingTransition(R.anim.left_right_anim, R.anim.translate);
                // close this activity
                finish();
                break;
        }
        return true;

    }

Solution

  • Create two variables.

    Handler handler;
    Runnable runnable;
    

    In your onCreate method do this.

    handler = new Handler;
    runnable = new Runnable() {
    // when user taps and move to next screen, after certain duration, this also 
     calls where two times WelcomePage is called.
    
                @Override
                public void run() {
                    // This method will be executed once the timer is over
                    // Start your app main activity
                    Intent i = new Intent(SplashScreen.this, WelcomePage.class);
                    startActivity(i);
                    //overridePendingTransition(R.drawable.from_middle, 
                R.drawable.to_middle);
                    overridePendingTransition(R.anim.left_right_anim, 
                 R.anim.translate);
                    // close this activity
                    finish();
                }
            }
    

    and then add the runnable to the handler

    handler.postDelayed(runnable,5000);
    

    And in the touch event

    case MotionEvent.ACTION_DOWN: handler.removeCallbacks(runnable);