【问题标题】:final or nulling views after use? What is more optimal? [closed]使用后的最终视图或无效视图?什么更优化? [关闭]
【发布时间】:2013-03-16 12:21:35
【问题描述】:

我想让我的应用尽可能优化。哪个代码更优化(速度、内存使用等),为什么?你怎么看;-)?

1.

    final Button testButton = (Button) findViewById(R.id.testButton);
    testButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
                 (some code here)
        }
    });

2.

    Button testButton = (Button) findViewById(R.id.testButton);
    testButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
                 (some code here)
        }
    });
    testButton = null;

ps。你有什么Android代码优化技巧吗?

【问题讨论】:

标签: android performance optimization


【解决方案1】:

我怀疑这是您应用的瓶颈。我根本不会担心这个。

另外,如果testButton 是一个局部变量,它会在方法返回时超出范围,并且将其设置为 null 将无效。在您的应用程序的一行代码中使用或不使用 final 将无法衡量。尝试制作用户实际需要更快等待的部分。

也就是说,我觉得其中包含 final 关键字使代码更具可读性。

【讨论】:

    【解决方案2】:

    我同意 vidstige。

    您刚刚在您的问题中添加了static,这没有任何意义。静态 final 仅用于常量。

    方法中的静态最终变量定义也无法编译!所以跳过它。 Final 就足够了,基本上是一种保险,如果你不想这样做,你就不会重用变量。

    【讨论】:

      猜你喜欢
      • 2014-10-31
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多