Skip to content

ParkSangGwon/TedOnActivityResult

Repository files navigation

What is TedOnActivityResult

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 Coroutines, you want synchronized like this.

val activityResult: ActivityResult = TedOnActivityResult.with().startActivityForResult(intent)

If you use RxJava, you want chaining like this.

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

TedOnActivityResult can make startActivityForResult() chaining.





Setup

  • Edit root/app/build.gradle like below.
  • You can choose only one library depend on your code style normal/coroutine/rxJava1/rxJava2
  • Replace x.y.z with the version shown in the 'Maven Central' button below, or the specific version you want (e.g. replace x.y.z with 3.3.0 if you want v3.3.0).

Maven Central

repositories {
  google()
  mavenCentral()
}

dependencies {
    // Normal
    implementation 'io.github.ParkSangGwon:tedonactivityresult:x.y.z'
    
    // Coroutine
    implementation 'io.github.ParkSangGwon:tedonactivityresult-coroutine:x.y.z'

    // RxJava2
    implementation 'io.github.ParkSangGwon:tedonactivityresult-rx1:x.y.z'
    // RxJava3
    implementation 'io.github.ParkSangGwon:tedonactivityresult-rx2:x.y.z'
}

If you think this library is useful, please press the star button at the top.



How to use

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();



Coroutine

If you use kotlin and coroutine, You can use suspend function.

val activityResult: ActivityResult = TedOnActivityResult.with(this).startActivityForResult(intent)



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);
      }

    });





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.

About

StartActivityForResult() / OnActivityResult() using Coroutine/RxJava

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published