Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Preview M: activity recreates after permission grant

I play around with the Preview M and test my app on it, especially the "saving a file to external storage" part. Before the download/save process starts, i request for

  Manifest.permission.WRITE_EXTERNAL_STORAGE

permission as described on the developer page: https://developer.android.com/preview/features/runtime-permissions.html

The dialog occurs as expected:

"Allow <AppName> to access photos, media, and files on your device?" Deny / Allow

If i hit the "deny"-button, the onRequestPermissionsResult-method of the activity is called.

If i press the "allow"-button, the activity is first recreated and afterwards the onRequestPermissionsResult-method is called. I think it's a result of the granted permission.

But the second situation is tricky because i want to trigger a callback that starts the download, but this object is null at this point:

public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {

    getLogger().error("onRequestPermissionsResult ( " + requestCode + ", " + permissions + ", " + grantResults + " )");
    switch (requestCode) {


        //permission for saving files?
        case PermissionCode.WRITE_EXTERNAL_STORAGE: {


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

                //HERE IS THE NULL-OBJECT 
                if (controller != null) {

                    controller.triggerCallback();
                }
            }
            break;
        }
        default: {

            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

So my questions are:

  1. Can I avoid this recreation?
  2. If no, how can i redesign my code to solve the problem - i'm totally idealess at the moment

EDIT 1:

I tried to solve the problem with a handler and postDelayed - but i skipped it.

EDIT 2:

I preliminary solved it and show a Toast to user with the request to push the download button again (in case of granted permission). But Google: ARE YOU SERIOUS?

EDIT 3:

No recreation happens with the latest SDK 6.0 (Version 23) - maybe somebody heard my weeping in Mountain View :-)

like image 242
A.D. Avatar asked Jul 15 '15 13:07

A.D.


1 Answers

You can always check the download condition immediately after the activity recreates itself in onCreate():

static final int MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
boolean initiateDownload = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState != null) {
        initiateDownload = savedInstanceState.getBoolean("toDownload");
    }
}

@Override
public void onResume() {
    super.onResume();
    final boolean hasPermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED;
    if(initiateDownload && hasPermission) {
        // start download here...
    } else {
        requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE},
                MainActivity.MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE) {
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            initiateDownload = true;
        } else {
            // denied permission...
        }
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("toDownload", initiateDownload);
}
like image 58
Neoh Avatar answered Oct 12 '22 08:10

Neoh