RxActivityResult


Source link: https://github.com/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 {

  compile 'com.github.VictorAlbertos:RxActivityResult:0.4.5-2.x'
  compile 'io.reactivex.rxjava2:rxjava:2.0.5' 
}

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 android.support.v4.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).

Resources

Boilerplate code suppressor tool for Android platform based on Project Lombok.

DobList is an Open Source Android library that provides to ListView adding ProgressBar (or any view) to the footer of ListView to be shown in loading more, and callback that is called when reaching last item in ListView.

There are more than one horizontal sliding menu library for Android, but what about a vertical sliding?

DobSliding is an Open Source Android library that allows developers to develop applications with vertical sliding menus just like the Android notifications menu.

greenrobot-common provides general purpose utilities for Android and Java projects. Having its root in the early days of Android development, the library is minimalistic, tiny in size (jar < 100k), and focuses on efficiency and performance.

This library provides a simple 2-way sync implementation to plug into your Android Sync framework integration.

A lightweight dependency injection framework for Android.

Inspired by MiniGuice and RoboGuice.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes