I have this code in the onCreate method:
ImageView iv01 = (ImageView)findViewById(R.id.hexagon01);
iv01.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View view, MotionEvent event) {
Intent intent = new Intent(view.getContext(), ChoiceActivity.class);
startActivity(intent);
return true;
}
});
When I touch the image the new activity loads correctly. I can go back pressing back button. But when I press the back button again, to close the app, it launches again the onTouch event, loading again the activity. How can I avoid it?
Thanks.
Honestly, the back button doesn’t launch new copies of your Activity. Your OnTouchListener launches a new copy of the Activity for each
ACTION_DOWN,ACTION_MOVE, andACTION_UPMotionEvent. But you only notice the numerous Activities when you try to close the active Activity…Simply ensure that you only launch the new Activity on one MotionEvent:
Or you could use an OnClickListener here instead.