Pankaj Kumar
Pankaj Kumar

Reputation: 83018

Marshmallow permission : Component Lifecycle

Looking into marshmallow permission modal, it calls onRequestPermissionsResult() before onResume() of Activity or Fragment.

Problem statement :

If we have any action performed on Action result (i.e. permission allowed), like I want to remove current fragment, then it is not possible because fragment and container activity both are into pause state. So is there any way to call onResume() before onRequestPermissionsResult() ?

Solution, I am using

I am using flags for permission, which is being reset on onRequestPermissionsResult() and being verified on onResume(). But this seems a confusing way in my case.

Upvotes: 3

Views: 1611

Answers (1)

Budius
Budius

Reputation: 39846

There's no way to change the order implemented those callbacks but you can easily hack-around it. I'll show two different ways:

Simply post:

private static final Handler uiHandler = new Handler(Looper.getMainLooper());

@Override
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults) {
    if(... condition ...)
        uiHandler.post(onPermission_condition);
}

private Runnable onPermission_condition = new Runnable(){
    @Override
    public void run(){
        ... do stuff here
    }
}

}

Use a flag:

private boolean shouldDoStuff = false;

@Override
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults) {
    if(... condition ...)
        shouldDoStuff = true;
}

@Override
public void onResume(){
  super.onResume();
  if(shouldDoStuff) {
     shouldDoStuff = false;
     ... do stuff here  
  }
}

Upvotes: 4

Related Questions