Hello, 

I have a problem about Button's onClick(). 

I startActivity() in onClick() handler. And if I tapped a button twice very 
quickly,
it started activity twice. (therefore there are two instances of the same 
activity).

I think it is (or should be) very simple for a button to start another 
activity and
following code snippet is the straight-forward way to do that.

<snippet>

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SubActivity.class));
}

});
}
}

</snippet>

To re-produce the undesired behavior, I added Thread.sleep(1000) in 
onClick().

<snippet>
@Override
public void onClick(View v) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
startActivity(new Intent(MainActivity.this, SubActivity.class));
}
</snippet>

Now, I can tap a button twice while it is shown as pressed and the 
SubActivity
is started twice.

I googled  for solution and found lots of hacks to avoid this 
double-tapping problem.
However, if it is required to do some special trick whenever I want to 
start an activity 
by clicking a button, it can be a big problem as a GUI framework.
(I cannot think of a framework which requires a special handling 
for every button click event to avoid double-clicking)

Is there a standard way of starting activity by clicking a button without 
worrying the double-clicking issues?

Thanks in advance. 

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to