tbruyelle / RxPermissions

Android runtime permissions powered by RxJava2
Apache License 2.0
10.48k stars 1.31k forks source link

Activity recreate problem #182

Open xchengDroid opened 7 years ago

xchengDroid commented 7 years ago

@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 commented 7 years ago

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

epool commented 7 years ago

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

xchengDroid commented 7 years ago

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

galuszkak commented 4 years ago

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