Skip to content
StartActivityForResult() / OnActivityResult() using RxJava
Java
Branch: master
Clone or download
ParkSangGwon Merge pull request #6 from theGnartist/master
Orientation Fixe for Android O
Latest commit 558aaee Jul 5, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app Change return Observable() -> Single() May 1, 2018
gradle/wrapper Initial commit Aug 22, 2017
tedonactivityresult-rx1 Change return Observable() -> Single() May 1, 2018
tedonactivityresult-rx2
tedonactivityresult Revert "Bug Fix: android O and later orientation fix" Jun 18, 2018
.gitignore Edit .ignore file Aug 22, 2017
.travis.yml Update .travis.yml Nov 7, 2017
README.md Update README.md May 1, 2018
build.gradle Added Kotlin build support Sep 6, 2017
dependencies.gradle Added Kotlin build support Sep 6, 2017
gradle.properties - activityRequestStack NPE handling May 25, 2018
gradlew Initial commit Aug 22, 2017
gradlew.bat Change return Observable() -> Single() May 1, 2018
publish.gradle Change return Observable() -> Single() May 1, 2018
settings.gradle

README.md

What is TedRxOnActivityResult

When we use startActivityForResult(), we have to receive result/data in onActivityResult().

 @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    
    startActivityForResult(intent,REQ_CODE_AAA);
    
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode){
      case REQ_CODE_AAA:
        if(resultCode==RESULT_OK){
          doSomething();
        }
        break;
      default:
        super.onActivityResult(requestCode, resultCode, data);    
    }
    
  }

This is very complex and inconvenience to us.

If you use RxJava, you want chaining like this.

  AA()
  .filter(...)
  .subscribeOn(...)
  .observeOn(...)
  .subscribe(...);

TedRxOnActivityResult can make startActivityForResult() chaining.





Setup

You can select library according to your RxJava version.

RxJava1

Download

dependencies {
    compile 'gun0912.ted:tedonactivityresult-rx1:x.y.z'
}

RxJava2

Download

dependencies {
    compile 'gun0912.ted:tedonactivityresult-rx2:x.y.z'
}

Normal

Even if you don't use RxJava, you can use this library.

Download

dependencies {
    compile 'gun0912.ted:tedonactivityresult:x.y.z'
}





How to use

RxJava

RxJava1/RxJava2 can write code like this.

TedRxOnActivityResult.with(this)
    .startActivityForResult(intent)
    .subscribeOn(...)
    .observeOn(...)
    .subscribe(activityResult -> {

      if (activityResult.getResultCode() == RESULT_OK) {
        Intent data = activityResult.getData();
        String name = data.getStringExtra(SampleActivity.EXTRA_NAME);
        int age = data.getIntExtra(SampleActivity.EXTRA_AGE, -1);
      }

    });

Normal

Also you can use OnActivityResultListener not RxJava's chaining style

TedOnActivityResult.with(this)
    .setIntent(intent)
    .setListener((resultCode, data) -> {
       if (resultCode == RESULT_OK) {
        String name = data.getStringExtra(SampleActivity.EXTRA_NAME);
        int age = data.getIntExtra(SampleActivity.EXTRA_AGE, -1);
      }

    })
    .startActivityForResult();





License

Copyright 2017 Ted Park

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You can’t perform that action at this time.