Skip to content
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 · 13 comments
Closed

Asking for permission in onResume might cause infinite loop #90

kenshin171 opened this issue Feb 12, 2016 · 13 comments

Comments

@kenshin171
Copy link

@kenshin171 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
Copy link
Member

@hotchemi hotchemi commented Feb 12, 2016

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

Loading

@mannodermaus
Copy link
Contributor

@mannodermaus mannodermaus commented Feb 12, 2016

Good catch!

Loading

@hotchemi
Copy link
Member

@hotchemi hotchemi commented Feb 21, 2016

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

Loading

@hotchemi hotchemi closed this Feb 21, 2016
@shiraji
Copy link
Member

@shiraji shiraji commented Feb 22, 2016

Another lint check? 😄

Loading

@hotchemi
Copy link
Member

@hotchemi hotchemi commented Feb 23, 2016

Lint master 😍

Loading

@codebased
Copy link

@codebased codebased commented Apr 10, 2016

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

Loading

@kenshin171
Copy link
Author

@kenshin171 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.

Loading

@codebased
Copy link

@codebased codebased commented Apr 10, 2016

yup thanks!

Loading

@t894924815
Copy link

@t894924815 t894924815 commented Aug 7, 2017

thanks!!!but why it behave like this?

Loading

@hotchemi
Copy link
Member

@hotchemi hotchemi commented Aug 7, 2017

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

Loading

@kenshin171
Copy link
Author

@kenshin171 kenshin171 commented Aug 7, 2017

More like this is how the android activity lifecycle works.

Loading

@farmazon3000
Copy link

@farmazon3000 farmazon3000 commented Apr 10, 2020

Why do we even need to start a dialog? Can't we show sth that doesn't trigger onPause/onResume?

Loading

@farmazon3000
Copy link

@farmazon3000 farmazon3000 commented Apr 10, 2020

Same issue on google samples:
android/location-samples#83

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants