Android 按下后退按钮时再次触发onTouch
我在onCreate方法中有以下代码:Android 按下后退按钮时再次触发onTouch,android,android-activity,Android,Android Activity,我在onCreate方法中有以下代码: ImageView iv01 = (ImageView)findViewById(R.id.hexagon01); iv01.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View view, MotionEvent event) { Intent intent = new Intent(view.ge
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;
}
});
当我触摸图像时,新活动将正确加载。我可以按后退按钮回去。但当我再次按下后退按钮关闭应用程序时,它会再次启动onTouch事件,再次加载活动。我怎样才能避免呢
谢谢。OnTouchListener被触发有几个原因(您需要检查MotionEvent参数才能找到确切的原因)。似乎你应该使用onclick监听器,以一种更简单的方式完成同样的事情 当我触摸图像时,新活动将正确加载。我可以按后退按钮回去。但当我再次按下后退按钮关闭应用程序时,它会再次启动onTouch事件,再次加载活动 老实说,后退按钮不会启动活动的新副本。OnTouchListener为每个
动作向下
、动作移动
和动作向上
运动事件启动活动的新副本。但是,当您试图关闭活动时,您只会注意到大量的活动
只需确保仅在一个MotionEvent上启动新活动:
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
Intent intent = new Intent(view.getContext(), ChoiceActivity.class);
startActivity(intent);
return true;
}
return false;
}
或者你可以在这里用一个