您的位置:首页 > 其它

防止多次快速点击打开多个activity的问题

2015-11-27 09:09 302 查看
防止多次快速点击打开多个activity的问题
  在Android开发中存在这样一个问题,如果一个按钮是打开一个activity界面的,如果用户在1秒钟之内快速点击这个按钮,那么就会打开多个相同的activity,这个是相当浪费资源的,下面介绍一种解决方法。

  
/** 判断是否是快速点击 */
private static long lastClickTime;

public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if (0 < timeD && timeD < 1000) {

return true;
}
lastClickTime = time;
return false;

}


然后在你的BaseActivity中加入下面代码:

/** 判断触摸时间派发间隔 */
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
if (Utils.isFastDoubleClick()) {
return true;
}
}
return super.dispatchTouchEvent(ev);
}


最后还有一步,在你有跳出来的activity中设置一下加载模式

android:launchMode="singleTask"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: