简体   繁体   English

如何阻止多个意图按顺序打开?

[英]How to stop multiple intents from opening sequentially?

I have a list of invoices displayed in one Java activity, and I included the ability to click on one of the invoices to open a new activity that shows its details (using an intent).我有一个显示在一个 Java 活动中的发票列表,并且我包括单击其中一个发票以打开一个显示其详细信息的新活动(使用意图)的能力。 The issue is, the new activity doesn't open immediately, and if the user clicks on an invoice(s) multiple times before one loads, they all get loaded on the screen and the user has to exit out of all of them.问题是,新活动不会立即打开,如果用户在加载之前多次点击发票,它们都会被加载到屏幕上,用户必须退出所有这些。

I have tried fiddling with the intent to fix this but I don't know where to start, I'm looking for some sort of function that can stop any on clicks from doing anything until the first one has loaded.我曾尝试摆弄以解决此问题,但我不知道从哪里开始,我正在寻找某种 function 可以阻止任何点击执行任何操作,直到第一个加载完成。 I'll include the code for my method below.我将在下面包含我的方法的代码。

        final ListView listView = root.findViewById(R.id.invoices_list);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent showDetailedInvoice = new Intent(getActivity(), DetailedInvoiceActivity.class);
                try {
                    Invoice invoice = new Invoice(invoices.getJSONObject(position));
                    showDetailedInvoice.putExtra("invoice", invoice);
                    showDetailedInvoice.putExtra("user", user);
                    startActivity(showDetailedInvoice);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

You have some approaches:你有一些方法:

1- Disabling items after the user clicks one time, so he can't click another one until the activity appears (so allowing just one) 1-在用户单击一次后禁用项目,因此在活动出现之前他不能单击另一个项目(因此只允许一个)

2- Controlling the clicks by time, for example one per second. 2- 按时间控制点击次数,例如每秒一次。 You will need to save last time user clicked and compare the current time with that and check at least one second passed.您将需要保存用户上次点击的时间并将当前时间与该时间进行比较,并检查至少经过一秒钟。 Probably the activity will open before 1 second.活动可能会在 1 秒前打开。 To do this you can use System.currentTimeInMillis(), so you will have something like:为此,您可以使用 System.currentTimeInMillis(),因此您将拥有如下内容:

private Long lastTimeUserClicked = null;

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Long clickTime = System.currentTimeInMillis();
    if(lastTimeUserclicked==null || clickTime - lasTimeUserClicked > 1000) { //1000 millisecs = 1 sec
      //Do your stuff ...
      lastTimeUserClicked = clickTime;   
    }
}

3- If the activity you are opening is only one and you change its data you can set the intent to be SingleTop with Intent flags: 3-如果您打开的活动只有一个并且您更改了它的数据,您可以将意图设置为带有 Intent 标志的 SingleTop:

Intent showDetailedInvoice = new Intent(getActivity(), DetailedInvoiceActivity.class);

showDetailedInvoice.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)

NOTE: Not sure if you will need clear top before.注意:不确定您之前是否需要透明顶部。 If it doesn't work with SINGLE_TOP only you can try to also add before如果它不适用于 SINGLE_TOP 只有你可以尝试在之前添加

showDetailedInvoice.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM