New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asking for permission in onResume might cause infinite loop #90

Closed
kenshin171 opened this Issue Feb 12, 2016 · 11 comments

Comments

Projects
None yet
6 participants
@kenshin171
Copy link

kenshin171 commented Feb 12, 2016

Hi, not exactly a bug, BUT i think this should be documented. Asking for permissions in onResume will cause an infinite loop when user ticks "never ask again" and denies the permission. After the user returns to the activity from the permissions dialog box onResume is called and it asks for permission again. It seems that onResume is always called after returning from a permission request check. Below is a very simple code snippet that demonstrates this issue.

    int i = 0;
    @Override
    protected void onResume() {
        super.onResume();
        MainActivityPermissionsDispatcher.enableLocationWithCheck(this);
        i++;
    }

    @NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION)
    void enableLocation() {
         //do something with location
    }

    @OnNeverAskAgain(Manifest.permission.ACCESS_FINE_LOCATION)
    void showNeverAskForLocation() {
        App.ToastMessage("Never ask again! "+i,this);
    }

This issue can be solved by putting the permission request call in onStart instead of onResume as onStart is called only once for the activity as documented in http://developer.android.com/training/basics/activity-lifecycle/pausing.html.
Below is the better place to call a method with permission request when you want the permission request to be asked on activity start.

  @Override
    protected void onStart() {
        super.onStart();
        MainActivityPermissionsDispatcher.enableLocationWithCheck(this);
    }

@hotchemi hotchemi added question and removed question labels Feb 12, 2016

@hotchemi

This comment has been minimized.

Copy link
Member

hotchemi commented Feb 12, 2016

Interesting, I've never tested that case. We'll investigate about it and do documentation. Thx!

@mannodermaus

This comment has been minimized.

Copy link
Contributor

mannodermaus commented Feb 12, 2016

Good catch!

@hotchemi

This comment has been minimized.

Copy link
Member

hotchemi commented Feb 21, 2016

I added this issue link to wiki. https://github.com/hotchemi/PermissionsDispatcher/wiki/Checklist
Thx for your report @kenshin171 !

@hotchemi hotchemi closed this Feb 21, 2016

@shiraji

This comment has been minimized.

Copy link
Member

shiraji commented Feb 22, 2016

Another lint check? 😄

@hotchemi

This comment has been minimized.

Copy link
Member

hotchemi commented Feb 23, 2016

Lint master 😍

@codebased

This comment has been minimized.

Copy link

codebased commented Apr 10, 2016

so whats the solution - where should be checked for required permissions ? I have just faced this problem

@kenshin171

This comment has been minimized.

Copy link
Author

kenshin171 commented Apr 10, 2016

Please refer to first post

This issue can be solved by putting the permission request call in onStart instead of onResume as onStart is called only once for the activity as documented in http://developer.android.com/training/basics/activity-lifecycle/pausing.html.
Below is the better place to call a method with permission request when you want the permission request to be asked on activity start.

@codebased

This comment has been minimized.

Copy link

codebased commented Apr 10, 2016

yup thanks!

@t894924815

This comment has been minimized.

Copy link

t894924815 commented Aug 7, 2017

thanks!!!but why it behave like this?

@hotchemi

This comment has been minimized.

Copy link
Member

hotchemi commented Aug 7, 2017

@t894924815 this is caused inside of Android permission framework so we dunno... 😅

@kenshin171

This comment has been minimized.

Copy link
Author

kenshin171 commented Aug 7, 2017

More like this is how the android activity lifecycle works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment