1

I am trying to implement dark theme from fragment named as "Fragment_One" in android studio. This is from that fragment file

                              if (!isChecked) {
                                   AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                                   isChecked = true;
                               } else {
                                   AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                                    isChecked = false;
                               }

It is giving error in MainActivity in the following line of onRequestPermissionsResult() method

    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

The onRequestPermissionsResult() method is as follows:

 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "permission granted on request", Toast.LENGTH_SHORT).show();
                files= getAllFiles(this);
                FragmentTransaction f_fragmentTransaction = getSupportFragmentManager().beginTransaction();
                f_fragmentTransaction.replace(R.id.mainFragment, new Fragment_Two()).commit();
            }
            if (grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            }
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]
                    {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_PERMISSION);
        }
        if (requestCode == 100) {
            if (grantResults.length > 0
                    && grantResults[0]  + grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this,"allow settings ",Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this,"permission denied ",Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
M Baig
  • 55
  • 7

0 Answers0