Search code examples
androidandroid-activityandroid-touch-event

Open an activity in a touch way android


In my first activity touching left or right opens the second activity Of the value calculation X and Y The activity is opened based on the conditions. This is part of the code

case MotionEvent.ACTION_MOVE:

    if (v.getId() == R.id.layout3) {

        float currentX = event.getX();
        float currentY = event.getY();

        if ((currentX > 160.0) && (currentY > 5.0) && (currentY < 55.0) &&(downXValue < 146.0)){       
            startActivity(i); 
        }
    }
    break;

So here everything goes well. The problem is that the activity opens repeatedly accumulating above each other If I want to go back to the first activity, you should press the button more than five times This means that the second activity opened more than five times. I am new at Android. I want to know where the problem is and what are the correct methods and where are the mistakes. Thank you


Solution

  • You could add a flag to check if the activity is already started:

    private activityStarted = false;
    
    if (!activityStarted && (currentX > 160.0) && (currentY > 5.0) && (currentY < 55.0) && (downXValue < 146.0)){   
        activityStarted = true;    
        startActivity(i); 
    }