Search code examples
javaandroidnavigation-drawerandroid-gradle-plugin-8.0

Resource IDs will be non-final by default in Android Gradle Plugin version 8.0, avoid using them in switch case statements


I have a warning in Android Studio about my navigation drawer resources. Warning is:

Resource IDs will be non-final by default in Android Gradle Plugin version 8.0, avoid using them in switch case statements.

I tried to use the method if to update my code but I won't 'converted right'. I found on the internet this article to help me to convert my code but it seem not work for me. I want to know if I miss something.

Down below is before and after to make an idea and here is my full activity because I saw lot of people have this after used public void onClick... and I don't use it. I have navigationView.setNavigationItemSelectedListener(menuItem -> {.

Before

navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (menuItem.getItemId())
            {
                case R.id.nav_drawer_settings:
                    Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_drawer_whitelist:
                    intent = new Intent (MainActivity.this, WhitelistActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_drawer_clipboard_cleaner:
                    intent = new Intent (MainActivity.this, ClipboardActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_drawer_invalid_media_cleaner:
                    intent = new Intent (MainActivity.this, InvalidActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_drawer_about:
                    intent = new Intent (MainActivity.this, AboutActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_drawer_support:
                    Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW,
                            Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
                    startActivity(newIntent);
                    break;
                case  R.id.nav_drawer_share:{
                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                    sharingIntent.setType("text/plain");
                    String shareBody =  "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
                    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                    startActivity(Intent.createChooser(sharingIntent, "Share using..."));
                }
                break;
            }
            return false;
        });
    }

After

navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (view.getId()) {
                if (item.getItemId() == R.id.nav_drawer_settings) {
                    Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_whitelist) {
                    Intent intent = new Intent (MainActivity.this, WhitelistActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_clipboard_cleaner) {
                    Intent intent = new Intent (MainActivity.this, ClipboardActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_invalid_media_cleaner) {
                    Intent intent = new Intent (MainActivity.this, InvalidActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_about) {
                    Intent intent = new Intent (MainActivity.this, AboutActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_support) {
                    Intent openURL = new Intent(Intent.ACTION_VIEW);
                    openURL.setData(Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
                    startActivity(openURL);
                }
                if (item.getItemId() == R.id.nav_drawer_share) {
                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                    sharingIntent.setType("text/plain");
                    String shareBody =  "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
                    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                    startActivity(Intent.createChooser(sharingIntent, "Share using..."));
                }
            }
            return false;
        });
    }

Thanks in advance! :D


Solution

  • On the after code that you posted, there is no need anymore for the switch.

    It should work only by writing the following:

                if (item.getItemId() == R.id.nav_drawer_settings) {
                    Intent intent = new Intent (MainActivity.this, SettingsActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_whitelist) {
                    Intent intent = new Intent (MainActivity.this, WhitelistActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_clipboard_cleaner) {
                    Intent intent = new Intent (MainActivity.this, ClipboardActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_invalid_media_cleaner) {
                    Intent intent = new Intent (MainActivity.this, InvalidActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_about) {
                    Intent intent = new Intent (MainActivity.this, AboutActivity.class);
                    startActivity(intent);
                }
                if (item.getItemId() == R.id.nav_drawer_support) {
                    Intent openURL = new Intent(Intent.ACTION_VIEW);
                    openURL.setData(Uri.parse("https://www.paypal.me/d4rkmichaeltutorials"));
                    startActivity(openURL);
                }
                if (item.getItemId() == R.id.nav_drawer_share) {
                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                    sharingIntent.setType("text/plain");
                    String shareBody =  "https://play.google.com/store/apps/details?id=com.d4rk.cleaner";
                    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Try right now!");
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                    startActivity(Intent.createChooser(sharingIntent, "Share using..."));
                }