Skip to content
This repository has been archived by the owner on Aug 9, 2020. It is now read-only.

Commit

Permalink
Added getOLiveActivity method
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor committed Mar 21, 2016
1 parent 57b159e commit d67b61b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
3 changes: 0 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ allprojects {
And add next dependencies in the build.gradle of the module:
```gradle
dependencies {
compile "com.github.VictorAlbertos:RxActivityResult:0.2.1"
compile "com.github.VictorAlbertos:RxActivityResult:0.2.2"
compile "io.reactivex:rxjava:1.1.0"
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import android.app.Application;
import android.os.Bundle;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Func1;

class ActivitiesLifecycleCallbacks {
private final Application application;
private Activity liveActivityOrNull;
Expand Down Expand Up @@ -45,4 +50,33 @@ private void registerActivityLifeCycle() {
Activity getLiveActivity() {
return liveActivityOrNull;
}

/**
* Emits just one time a valid reference to the current activity
* @return the current activity
*/
private boolean emitted = false;
Observable<Activity> getOLiveActivity() {
emitted = false;
return Observable.interval(50, 50, TimeUnit.MILLISECONDS)
.map(new Func1<Long, Activity>() {
@Override public Activity call(Long aLong) {
return liveActivityOrNull;
}
})
.takeWhile(new Func1<Activity, Boolean>() {
@Override public Boolean call(Activity activity) {
boolean continueEmitting = true;
if (emitted) continueEmitting = false;
if (activity != null) emitted = true;
return continueEmitting;
}
})
.filter(new Func1<Activity, Boolean>() {
@Override public Boolean call(Activity activity) {
return activity != null;
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;

public class RxActivityResult {
private static ActivitiesLifecycleCallbacks activitiesLifecycle;
Expand Down Expand Up @@ -67,8 +68,11 @@ public Observable<Result<T>> startIntent(final Intent intent) {
OnResult onResult = uiTargetActivity ? onResultActivity() : onResultFragment();
HolderActivity.setRequest(new Request(intent, onResult));

Activity activity = activitiesLifecycle.getLiveActivity();
activity.startActivity(new Intent(activity, HolderActivity.class));
activitiesLifecycle.getOLiveActivity().subscribe(new Action1<Activity>() {
@Override public void call(Activity activity) {
activity.startActivity(new Intent(activity, HolderActivity.class));
}
});

return observable;
}
Expand Down

0 comments on commit d67b61b

Please sign in to comment.