java - Android permissions - regarding permission request conflicts -


i have following code request read_ext_storage permission in activity's oncreate():

if(build.version.sdk_int >= build.version_codes.m) {         if (contextcompat.checkselfpermission(this, manifest.permission.read_external_storage) != packagemanager.permission_granted) {             if (activitycompat.shouldshowrequestpermissionrationale(this,                     manifest.permission.read_external_storage)) {                 // show expanation user *asynchronously* -- don't block                 // thread waiting user's response! after user                 // sees explanation, try again request permission.                 alertdialog.builder adb = new alertdialog.builder(this);                 adb.setmessage("this app needs read external storage/gallery provide images , music you're going use in video.").setpositivebutton("ok", null).create();                 adb.show();             } else {                  // no explanation needed, can request permission.                  activitycompat.requestpermissions(this,                         new string[]{manifest.permission.read_external_storage},                         my_permissions_request_read_ext_storage);                  // my_permissions_request_read_contacts                 // app-defined int constant. callback method gets                 // result of request.             }         }         else         {             oncreateactual();         }     } 

where oncreateactual() contains actual code executed in oncreate(). includes setting fragments dynamically.

onstartactual() involves reading external storage picture album names , putting them in listview in fragment.

this in onrequestpermissionsresult():

private int my_permissions_request_read_ext_storage = 102;  @override public void onrequestpermissionsresult(int requestcode, string[] permissions, int[] grantresults) {     if(build.version.sdk_int >= build.version_codes.m) {         log.i("perms", permissions.length+"");         int indexofreadstorage = 0;         for(int = 0 ; < permissions.length; i++)         {             log.i("perms_len", permissions[i]);             if(permissions[i].contains("read_external_storage"))             {                 indexofreadstorage = i;             }         }         if (grantresults.length > 0 && grantresults[indexofreadstorage] == packagemanager.permission_granted) {             // permission granted             log.i("read_ext_storage_perm", "granted");             oncreateactual();         } else {             // permission denied             toast.maketext(this, "read_ext_storage denied", toast.length_short)                     .show();         }     }     super.onrequestpermissionsresult(requestcode, permissions, grantresults); } 

my problem have advertising library that's asking read_external_storage permission, , if put oncreateactual() before instead of within else clause, library asks permission first, , onrequestpermissionsresult() above won't called.

if this, however, getsupportfragmentmanager().begintransaction().add(r.id.folders_section, ctrlfragone, "album_search_results").add(r.id.selection_section, ctrlfragtwo, "indicator").commit(); nets me:

java.lang.illegalstateexception: can not perform action after onsaveinstancestate

so, unless there's better way, think have somehow stall lifecycle of activity until read_external_storage permission granted.

is possible stall activity's lifecycle until, say, variable changed? if not, can refresh fragment?

i think should try ask works , takes out of hastle

just compile dependency

compile 'com.vistrav:ask:2.1' 

and here full example

public class mainactivity extends appcompatactivity {  private static final string tag = mainactivity.class.getsimplename();  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     ask.on(this)             .forpermissions(manifest.permission.access_coarse_location                     , manifest.permission.write_external_storage)             .withrationales("location permission need map work properly",                     "in order save file need grant storage permission") //optional             .go(); }  //optional @askgranted(manifest.permission.write_external_storage) public void fileaccessgranted() {     log.i(tag, "file  granted"); }  //optional @askdenied(manifest.permission.write_external_storage) public void fileaccessdenied() {     log.i(tag, "file  denied"); }  //optional @askgranted(manifest.permission.access_coarse_location) public void mapaccessgranted() {     log.i(tag, "map granted"); }  //optional @askdenied(manifest.permission.access_coarse_location) public void mapaccessdenied() {     log.i(tag, "map denied"); } } 

you can find more details here

hope helps


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -