Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Fix IllegalStateException and add support for fragments. (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
epool authored Jul 10, 2018
1 parent a7c81c3 commit c9b8170
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ allprojects {
}
dependencies {
implementation 'com.github.tbruyelle:rxpermissions:0.10.1'
implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
}
```

Expand All @@ -26,9 +26,11 @@ dependencies {
Create a `RxPermissions` instance :

```java
RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity instance
final RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity or Fragment instance
```

**NOTE:** `new RxPermissions(this)` the `this` parameter can be an Activity or a Fragment. If you are using `RxPermissions` inside of a fragment you should pass the fragment instance(`new RxPermissions(this)`) as constructor parameter rather than `new RxPermissions(fragment.getActivity())` or you could face a `java.lang.IllegalStateException: FragmentManager is already executing transactions`.

Example : request the CAMERA permission (with Retrolambda for brevity, but not required)

```java
Expand Down
20 changes: 12 additions & 8 deletions lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
Expand All @@ -41,32 +42,35 @@ public class RxPermissions {
Lazy<RxPermissionsFragment> mRxPermissionsFragment;

public RxPermissions(@NonNull final FragmentActivity activity) {
mRxPermissionsFragment = getLazySingleton(activity);
mRxPermissionsFragment = getLazySingleton(activity.getSupportFragmentManager());
}

public RxPermissions(@NonNull final Fragment fragment) {
mRxPermissionsFragment = getLazySingleton(fragment.getChildFragmentManager());
}

@NonNull
private Lazy<RxPermissionsFragment> getLazySingleton(@NonNull final FragmentActivity activity) {
private Lazy<RxPermissionsFragment> getLazySingleton(@NonNull final FragmentManager fragmentManager) {
return new Lazy<RxPermissionsFragment>() {

private RxPermissionsFragment rxPermissionsFragment;

@Override
public synchronized RxPermissionsFragment get() {
if (rxPermissionsFragment == null) {
rxPermissionsFragment = getRxPermissionsFragment(activity);
rxPermissionsFragment = getRxPermissionsFragment(fragmentManager);
}
return rxPermissionsFragment;
}

};
}

private RxPermissionsFragment getRxPermissionsFragment(@NonNull FragmentActivity activity) {
RxPermissionsFragment rxPermissionsFragment = findRxPermissionsFragment(activity);
private RxPermissionsFragment getRxPermissionsFragment(@NonNull final FragmentManager fragmentManager) {
RxPermissionsFragment rxPermissionsFragment = findRxPermissionsFragment(fragmentManager);
boolean isNewInstance = rxPermissionsFragment == null;
if (isNewInstance) {
rxPermissionsFragment = new RxPermissionsFragment();
FragmentManager fragmentManager = activity.getSupportFragmentManager();
fragmentManager
.beginTransaction()
.add(rxPermissionsFragment, TAG)
Expand All @@ -75,8 +79,8 @@ private RxPermissionsFragment getRxPermissionsFragment(@NonNull FragmentActivity
return rxPermissionsFragment;
}

private RxPermissionsFragment findRxPermissionsFragment(@NonNull FragmentActivity activity) {
return (RxPermissionsFragment) activity.getSupportFragmentManager().findFragmentByTag(TAG);
private RxPermissionsFragment findRxPermissionsFragment(@NonNull final FragmentManager fragmentManager) {
return (RxPermissionsFragment) fragmentManager.findFragmentByTag(TAG);
}

public void setLogging(boolean logging) {
Expand Down

0 comments on commit c9b8170

Please sign in to comment.