RxPermissions icon indicating copy to clipboard operation
RxPermissions copied to clipboard

Activity recreate problem

Open xchengDroid opened this issue 7 years ago • 4 comments

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ac_main);
    RxPermissions rxPermissions = new RxPermissions(this);
    rxPermissions.request(Manifest.permission.CAMERA).subscribe(new Consumer<Boolean>() {
        @Override
        public void accept(Boolean aBoolean) throws Exception {
            Log.e("print", "accept:" + this);
        }
    });
}

when MainActivity's screenOrientation changed and recreated , I click allow permission and accept(Boolean aBoolean) method will be called 3 times ,can you tell me how to resolve this problem

xchengDroid avatar Nov 07 '17 11:11 xchengDroid

In RxPermissions.class ,the method requestImplementation(final String... permissions) has a bug that will always reference Activity instance when activity recreate

PublishSubject<Permission> subject = mRxPermissionsFragment.getSubjectByPermission(permission);
// Create a new subject if not exists
if (subject == null) {
    unrequestedPermissions.add(permission);
    subject = PublishSubject.create();
    mRxPermissionsFragment.setSubjectForPermission(permission, subject);
}

list.add(subject);

you can change it like this

PublishSubject<Permission> subject = mRxPermissionsFragment.getSubjectByPermission(permission);
// Create a new subject if not exists
if (subject == null) {
    unrequestedPermissions.add(permission);
}
subject = PublishSubject.create();
mRxPermissionsFragment.setSubjectForPermission(permission, subject);

list.add(subject);

It will override next same permission that not reference the Activity instance and never called callback more than once when it destroyed

sorry for my bad english

Thank you for your perusal

xchengDroid avatar Nov 07 '17 14:11 xchengDroid

@xchengDroid could you share a project where we can be able to reproduce this issue?

epool avatar Nov 08 '17 04:11 epool

@epool It is very simple to reproduce this issue! you just start an activity that make request permission at onCreate method then make it change screen orientation ,it will recreate, and click allow permission, the callback will be called more than once.

xchengDroid avatar Nov 08 '17 05:11 xchengDroid

@xchengDroid I think you probably missing disposing inside onDestroy method in activity.

Example is available here: https://github.com/tbruyelle/RxPermissions/blob/master/sample/src/main/java/com/tbruyelle/rxpermissions2/sample/MainActivity.java#L83-L89

Because You aren't disposing this Observable it is created twice and that's why You have multiple callbacks.

galuszkak avatar Jul 10 '20 05:07 galuszkak