开发者

Android onTap start Activity

I have the following code that starts an activity after detecting an onTap event.

Trouble is that more often than not it detects 2-3 taps from one user just pressing and so starts multiple. Is there any way to dampen the effect and only open the once?

protected boolean onTap(int i) {

            Log.i("TAP","CALLED");


            myOverlayItem item =getItem(i);

            if (selectedName.equals(item.getTitle())){


                try{    
                Intent myIntent = new Intent(AppMain.this, DetailActivity.class);
                myIntent.putExtra( "int", i);
                myIntent.putExtra( "string", selectedName );
                AppMain.this.startActivity(myIntent);
                }catch (Exception e){
                    Log.e("E开发者_JS百科rror", "Cannot launch", e);
                }
            }
}

Thanks!


Try setting a boolean to true so you can make sure the code only runs once

boolean activityIsAlreadyRunning;


if(activityIsAlreadyRunning == false){
    if (selectedName.equals(item.getTitle())){
        try{    
            Intent myIntent = new Intent(AppMain.this, DetailActivity.class);
            myIntent.putExtra( "int", i);
            myIntent.putExtra( "string", selectedName );
            AppMain.this.startActivity(myIntent);

            activityIsAlreadyRunning = true;

            }catch (Exception e){
                Log.e("Error", "Cannot launch", e);
            }
    }
}


You can also try reading the android:launchMode. This is how to launch your activity by single or multiple instance.


Why can't you issue an onClickListener? This would launch just the once when clicked. Another possible solution might also be to simply set a public boolean variable within your parent class, and when your function is launched simply set isRunning = true; and right before it finishes it can set isRunning = false;. This would allow you to encase the launching of the event in a block as such:

if (!isRunning){
    onTap(integer);
}

This would make it so the method wasn't run on a touch, unless it wasn't currently already running. If this doesn't help, you could always compare your touch events event.x & event.y with the difference in event.getEventTime(). Then it could look something like this within your onTouchEvent:

if ((Math.abs(event.x - mLastEventX) > 2.0f) || 
        (Math.abs(event.y - mLastEventY) > 2.0f) || 
             ((event.getEventTime() - mLastEventTime) > 500f)){
    onTap(integer);
}

This would basically make it so it wouldn't launch unless any of the following were true:

  • ((Math.abs(event.x - mLastEventX) > 2.0f) or X value changes more than 2
  • (Math.abs(event.y - mLastEventY) > 2.0f) or Y value changes more than 2
  • ((event.getEventTime() - mLastEventTime) > 500f) or the time has increased 500 millis

Without any more information as to what exactly you're trying to accomplish it's pretty hard for me to give you a solutions fit just for you. If none of these work though, try putExtras / getExtras which deal with using an intent to put and get information throughout activity starts and finishes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜