-1

Je fais un écran de démarrage dans lequel après une certaine durée, il passe à un autre écran. Si l'utilisateur ne souhaite pas attendre cette durée, il peut appuyer sur l'écran de démarrage pour passer à un autre écran. Mais le problème est, lorsque l'utilisateur tape sur l'écran de démarrage, il va à un autre écran. Simlutanément automatique pour certaine durée fonctionne également de sorte que, deux fois deuxième écran s'ouvre. Comment surmonter ce problème? Ci-dessous est le code que j'ai essayé où j'ai besoin de gérer le robinet et la commutation automatique à un autre écran, mais il échoue.Comment gérer la commutation d'écran automatique et également toucher l'événement pour passer à un autre écran android?

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; 

    } 
+0

ajouter 'handler.removecallbacksandmessages (null)' en cas de commutation – Raghunandan

Répondre

0

Créez deux variables.

Handler handler; 
Runnable runnable; 

Dans votre méthode onCreate, procédez comme suit.

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

puis ajoutez le runnable au gestionnaire

handler.postDelayed(runnable,5000); 

Et en cas toucher

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