简体   繁体   English

棉花糖许可:组件生命周期

[英]Marshmallow permission : Component Lifecycle

Looking into marshmallow permission modal, it calls onRequestPermissionsResult() before onResume() of Activity or Fragment. 查看marshmallow权限模式,它在Activity或Fragment的onResume()之前调用onRequestPermissionsResult()

Problem statement : 问题陈述 :

If we have any action performed on Action result (ie permission allowed), like I want to remove current fragment, then it is not possible because fragment and container activity both are into pause state. 如果我们对Action结果执行了任何操作(即允许权限),就像我想删除当前片段一样,那么它是不可能的,因为片段和容器活动都处于暂停状态。 So is there any way to call onResume() before onRequestPermissionsResult() ? 那么有没有办法在onRequestPermissionsResult()之前调用onResume() onRequestPermissionsResult()

Solution, I am using 解决方案,我正在使用

I am using flags for permission, which is being reset on onRequestPermissionsResult() and being verified on onResume() . 我正在使用标志进行许可,正在onRequestPermissionsResult()上重置并在onResume()上进行验证。 But this seems a confusing way in my case. 但在我看来,这似乎令人困惑。

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  
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM