本文介绍了它是安全的重用意图是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而Android文档定义一个Intent为含执行的操作的抽象描述信息的包。这表明,你应该能够在需要多次重复使用一个单一的意向对象,但我还没有看到任何例子,说明这种情况下/做是安全的。有什么理由不做到以下几点:

The Android docs define an Intent as "a bundle of information containing an abstract description of an operation to perform". This suggests that you should be able to reuse a single Intent object multiple times if needed, but I haven't seen any examples showing this is the case/ is safe to do. Is there any reason to NOT do the following:

private final Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
...
protected void onCreate(Bundle savedInstanceState) {
  enabledBluetoothIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
  ...
}

,然后调用 startActivityForResult(enableDiscoverableIntent,REQUEST_ENABLE_BT_DISCOVERY)在code多的地方?如果同样的意图启动了两次会发生什么?

and then call startActivityForResult(enableDiscoverableIntent, REQUEST_ENABLE_BT_DISCOVERY) in multiple places in the code? What happens if the same intent is started twice?

推荐答案

当你想用它做同样的事情,这是完全安全的,因为一个意图是不大于一串数据和指令的更多。如果您想使用相同的意图对象为不同的目的(例如你有一堆选项卡,并尝试设置标签重复使用相同的意图,但改变他们'活动LL推出),你必须要更加小心了,我建议你重新创建一个新的意图对象为每。

It is completely safe when you want to use it to do the exact same thing, since an Intent is no more than a bunch of data and instructions. If you want to use the same Intent object for different purposes (for example you have a bunch of tabs and try to set the tabs reusing the same intent but changing the activity they'll launch) you have to be more careful, and I'd recommend re-creating a new Intent object for each.

这篇关于它是安全的重用意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:44
查看更多