RxPermissions: Question about "Must be done during an initialization phase like onCreate"
Why do I need to get the instance of RxPermission in a phase like onCreate? Since internally it uses the ApplicationContext isn’t the activity lifecycle irrelevant?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 15 (7 by maintainers)
Main point is not that your activity should be visible, but that you have to invoke these methods during initialization phase (i.e. onCreate()/onStart() in Activity, etc.)
For example, if you call request() and subscribe to observable in OnClickListener of some button. User taps button, and permissions request is started. If during permissions request activity is recreated (i.e. due to configuration changes), then code in you click listener is not executed again and you don’t subscribe to observable - permissions request result is lost.
But if you subscribe to observable during init phase, then in case of activity recreation, you’ll resubscribe, and receive result from observable.
I linked this issue in the readme.
The sample app has the subscription in
onCreate()but no corresponding call tounsubscribe(). Does it matter that the subscription is leaked here?FWIW I can reproduce this even if the system decides to not show the system permission dialog. (i.e. Android OS is still pausing my Activity, and thus Fragment, every time I request permission. Following advice in the readme fixed the problem
So basically when calling
request,ensureor the methods alike ofRxPermissionI need to have an activity that is visible?