如何在Android中仅标记活动的一个实例? [英] How to flag only one instance of an activity in android?

查看:63
本文介绍了如何在Android中仅标记活动的一个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android应用程序中,我有一个按钮,单击该按钮可打开另一个活动.问题在于,用户可能会快速多次多次点击该按钮,这将打开许多这些新活动.我该如何强制使其一次只能打开其中一项活动?

In my android application, I have a button that, when clicked opens another activity. The problem is that the user could keep tapping on that button 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.

推荐答案

如果情况像您描述的那么简单,则可以使用

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);
    }
});

快速按下按钮将创建许多CalledActivity实例而没有Intent标志.

Rapid-pressing on the button will create many instances of CalledActivity without the Intent flag.

这篇关于如何在Android中仅标记活动的一个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆