• This repository has been archived on 09/Aug/2020
  • Stars
    star
    595
  • Rank 75,217 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A reactive-tiny-badass-vindictive library to break with the OnActivityResult implementation as it breaks the observable chain.

⚠️ Deprecated ⚠️

This library is no longer mantained. You should use the Android Navigation component with NavBackStackEntry for returning a result to the previous Destination.

Android Arsenal

Join the chat at https://gitter.im/VictorAlbertos/RxActivityResult

RxActivityResult

The api which Android SDK exposes to retrieve the data from a 'returning system call' (camera, gallery, email...) just does not give a shit about Don't break the chain leitmotiv. Indeed, the OnActivityResult approach will break entirely your observable chaining.

I did this library to not have to deal with this OnActivityResult pattern. Never. Ever.

RxActivityResult features:

  • Launch the intent from any class, as long as you supply a valid Activity or Fragment instance.
  • Get the Intent back with the data encapsulated in an observable and keep going crazy chaining operators.

Setup

Add the JitPack repository in your build.gradle (top level module):

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

And add next dependencies in the build.gradle of the module:

dependencies {
    implementation 'com.github.VictorAlbertos:RxActivityResult:0.5.0-2.x'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.3'
}

Usage

Call RxActivityResult.register in your Android Application class, supplying as parameter the current instance.

public class SampleApp extends Application {

    @Override public void onCreate() {
        super.onCreate();
        RxActivityResult.register(this);
    }
}

You can call RxActivityResult.on(this).startIntent(intent) supplying both, an Activity instance or a Fragment instance. Observe the emitted Result item to know the resultCode and retrieve the associated data if appropriate.

Limitation: Your fragments need to extend from androidx.fragment.app.Fragment instead of android.app.Fragment, otherwise they won't be notified.

Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

RxActivityResult.on(this).startIntent(takePhoto)
        .subscribe(result -> {
            Intent data = result.data();
            int resultCode = result.resultCode();
            // the requestCode using which the activity is started can be received here.
            int requestCode = result.requestCode();

            if(requestCode == YourActivity.YOUR_REQUEST_CODE)
            {
                // Do Something
            }

            if (resultCode == RESULT_OK) {
                result.targetUI().showImage(data);
            } else {
                result.targetUI().printUserCanceled();
            }
        });

Please pay attention to the targetUI() method in the Result object emitted.

This method returns a safety instance of the current Activity/Fragment. Because the original one may be recreated (due to configuration changes or some other system events) it would be unsafe calling it.

Instead, you must call any method/variable of your Activity/Fragment from this instance encapsulated in the Result object.

StartIntentSenderForResult

RxActivityResult supports startIntentSenderForResult too, by calling RxActivityResult.on(this).startIntentSender and supplying the proper arguments. As follows:

RxActivityResult.on(this).startIntentSender(pendingIntent.getIntentSender(), new Intent(), 0, 0, 0)
        .subscribe(result -> {
            
        });

Examples

There is an example of RxActivityResult using both activity and fragment in the app module

Author

Víctor Albertos

Another author's libraries using RxJava:

  • RxCache: Reactive caching library for Android and Java.
  • RxPaparazzo: RxJava extension for Android to take images using camera and gallery.
  • RxFcm: RxJava extension for Android Firebase Cloud Messaging (aka fcm).

More Repositories

1

RxCache

Reactive caching library for Android and Java
Java
2,375
star
2

BreadcrumbsView

A customizable Android view for paginated forms
Java
684
star
3

RxSocialConnect-Android

OAuth RxJava extension for Android.
Java
257
star
4

SwipeCoordinator

A coordinator layout for Android views to animate and typify touch events as swipe gestures
Java
253
star
5

ReactiveCache

A reactive cache for Android and Java which honors the reactive chain.
Java
242
star
6

Mockery

Android and Java library for mocking and testing networking layers with built-in support for Retrofit.
Java
145
star
7

RxCacheSamples

How to use RxCache for both Android and Java projects
Java
116
star
8

RxFcm

RxJava extension for Android Firebase Cloud Messaging (aka fcm).
Java
115
star
9

RxPermissionsResult

Ask for permissions without breaking the observable chain.
Java
109
star
10

DeviceAnimationTestRule

A JUnit rule to disable and enable device animations
Kotlin
106
star
11

RxGcm

[DEPRECATED] RxJava extension for Android Google Cloud Messaging (aka gcm).
Java
95
star
12

Jolyglot

Agnostic Json abstraction to perform data binding operations for Android and Java.
Java
70
star
13

RestAPIParseAuthCleanAndroid

An android implementation of an authentication system based on the Parse Api using the design patters suggested by Robert C. Martin (aka Uncle Bob) on his “clean architecture”.
Java
45
star
14

KDirtyAndroid

A dirty approach for truly client Android applications using Kotlin
Kotlin
17
star
15

DirtyAndroid

A dirty approach for truly client Android applications
Java
15
star
16

RxLifecycleInterop

[DEPRECATED] Workaround for using RxLifecycle with RxJava2
Java
10
star
17

Pacman

The classic video game Pacman for iOS and Android made with Unity 2d
C#
9
star
18

ml_technical_analysis

A machine learning approach integrating technical analysis to forecast stock prices
Python
8
star
19

EventBus-Samples

A simple demonstration about how to use EventBus with Activities, Fragments and Services on Android.
Java
7
star
20

android-kotlin-adoption

Kotlin adoption on the Android open source community
Python
6
star
21

Near-friends

App to know where your contacts are at every time
Java
3
star
22

bs-blockchain-hackathon-team4

BS Blockchain Hackathon Team 4 Repo
JavaScript
2
star
23

github-pr-latency-ml

A failed attempt (so far) to predict Github Pull Request latency.
Python
1
star
24

kotlin-apuntes

Kotlin
1
star