Question

I'm trying to understand if the user has enabled the admin or not and LATER update a checkbox (in onResume).

The problem is that the activity which allows the user to enable the admin is launched and it's launched the following code without waiting for the user decision. How could bypass it?

private void doAttivaRimuoviAdmin() {
    if (isAdminAttivo()) {
        mDPM.removeActiveAdmin(mDeviceAdminSample);
    } else {
        Intent localIntent = new Intent("android.app.action.ADD_DEVICE_ADMIN");
        localIntent.putExtra("android.app.extra.DEVICE_ADMIN", mDeviceAdminSample);
        localIntent.putExtra("android.app.extra.ADD_EXPLANATION",
                getString(R.string.spiegazione_amministratore));
        startActivityForResult(localIntent, 1);

        // se non è stato dato il permesso, non attiva la checkbox
        Editor e = mPrefs.edit();

        if (isAdminAttivo()) {
            e.putBoolean("spegnischermoabilitato", true);
        } else {
            e.putBoolean("spegnischermoabilitato", false);
        }
        e.commit();
        Log.i(getString(R.string.app_name), ""+ mPrefs.getBoolean("spegnischermoabilitato", false));

    }

}

In poor words, the sharedpreference "spegnischermoabilitato" always has FALSE within it.

Was it helpful?

Solution

From the documentation of startActivityForResult():

Launch an activity for which you would like a result when it finished. When this activity exits, your onActivityResult() method will be called with the given requestCode. Using a negative requestCode is the same as calling startActivity(Intent) (the activity is not launched as a sub-activity).

In other words, you will need to override onActivityResult(). In this method you will then have to check that the method is called with the requestCode you set in startActivityForResult(), and also that it contains the expected resultCode. If it does write your preference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top