如何旗活动只有一个实例在Android的? [英] How to flag only 1 instance of activity in android?
问题描述
在我的Android应用程序,我有一个按钮,点击后会打开另一个活动。的问题是,用户能保持迅速该按钮多次攻丝和它将打开很多这些新的活动。我怎么能强迫它,以便只有一个这些活动都可以在同一时间开?
In my android app, I have a button that when clicked opens another activity. The problem is that the user could keep tapping on that button for many times quickly and it would open a lot of those new activities. How can I force it so that only one of those activities can be open at a time?
我想避免办大事像禁用按钮,或将载入画面,
I want to avoid doing big things like disabling buttons or putting loading screens,
推荐答案
如果该情况是因为你描述的那么简单,你可以启动使用的 FLAG_ACTIVITY_SINGLE_TOP
。将prevent新实例被创建,如果有一个已经被证明。粗例如:
If the scenario is as simple as you describe, you can launch the Activity with FLAG_ACTIVITY_SINGLE_TOP
. That will prevent new instances from being created if one has already been shown. A crude example:
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, CalledActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
}, 1000);
}
});
速效pressing上的按钮将创建 CalledActivity
很多情况下没有意图的标志。
Rapid-pressing on the button will create many instances of CalledActivity
without the Intent flag.
这篇关于如何旗活动只有一个实例在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!