Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve "Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them in switch case statements" warning? [duplicate]

 @Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.legalInformationLL:
            startActivity(new Intent(AboutActivity.this, LegalInformation.class));
            break;
        case R.id.backIB:
            finish();
            break;
    }
}

for this code "Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them in switch case statements" warning shows up. What is the possible solution? If I change this code to:

 @Override
public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.legalInformationLL) {
        startActivity(new Intent(AboutActivity.this, LegalInformation.class));
    } else if (id == R.id.backIB) {
        finish();
    }
}

warning goes; but switch statement is better for performance as compared to if statement. So what is the possible solution that will work efficiently faster?

like image 687
Chaitanya Karmarkar Avatar asked Oct 13 '20 12:10

Chaitanya Karmarkar


1 Answers

The issue happens because since ADT 14 resource identifiers are no longer final.

See the next link, where Google states to use instead "if/else" conditions as alternative:

http://tools.android.com/tips/non-constant-fields

That being said. when it comes to performance, "switch" statements may perform better than "if/else" conditions.

However, in your case, you do not gain or lose performance or efficiency.

The type of performance that a "switch" statement may give, has to be taken into consideration for more specific edge cases which may require high efficiency, such as render loops, or algorithms with efficiency in focus.

For your use-case, using an "if/else" condition is a good solution without efficiency issues.

like image 186
PerracoLabs Avatar answered Nov 20 '22 00:11

PerracoLabs