Inconsistent behavior after isExternalStorageManager() = true in Android 11

410 Views Asked by At

I added a Preference button in my PreferenceScreen to call for Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION in Android 11:

        prefn.setOnPreferenceClickListener(arg0 -> {
            Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            Uri uri = Uri.fromParts("package", getPackageName(), null);
            intent.setData(uri);
            startActivity(intent);
            return true;
        });

        if (Environment.isExternalStorageManager()) {
            pref.setSummary("Allowed");
        } else {
            pref.setSummary("Not Allowed");
        }

When I change the setting from Allow to Not allow and the setting screen is closed, the PreferenceScreen is reloaded (in particular, the onCreate()), and the pref summary changes to "Not Allowed", as expected. However, if I click and change the setting from Not allow to Allow, it is not reloaded, and the summary still shows "Not allowed" despite the fact that the setting is in the Allow state. Even stranger, if I change 3 times, Not allow to Allow to Not allow to Allow, it is reloaded and the summary shows "Allowed", as it should.

Any clue?? I tried calling startActivityForResult without success.

1

There are 1 best solutions below

0
On

This is not a solution, but a workaround. And a coward one, sorry.

Simply fix the thing through the onResume() method of the Preference activity. Namely, before calling Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, set a variable:

lastManagerPerm = Environment.isExternalStorageManager();

and then in the onResume():

public void onResume() {
    super.onResume();
    if (Build.VERSION.SDK_INT >= 30 && !lastManagerPerm && Environment.isExternalStorageManager()) {
         prefn.setSummary("Allowed");
         ....
    }
}