Skip to content
OnActivityResult annotation compiler for Android
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
.buildscript Don't clean build again when deploying SNAPSHOTS. (#94) Aug 20, 2017
code_quality_tools Remove Code Quality Tools for Kotlin as there's no Kotlin in here. (#115 May 9, 2018
gradle/wrapper Update Gradle wrapper to 4.8 (#117) Jun 10, 2018
onactivityresult-annotations Use Gradle Maven Publish Plugin for publishing. (#116) May 25, 2018
onactivityresult-compiler Use Gradle Maven Publish Plugin for publishing. (#116) May 25, 2018
onactivityresult-sample Tweak Travis configuration. (#112) Mar 10, 2018
onactivityresult Use Gradle Maven Publish Plugin for publishing. (#116) May 25, 2018
.travis.yml Remove sudo: false from travis config. (#118) Nov 25, 2018 Prepare next development version. Dec 10, 2017
LICENSE Initial commit Dec 12, 2015 Nuke badges in README. (#114) Mar 29, 2018
build.gradle Update Gradle wrapper to 4.8 (#117) Jun 10, 2018 First version Dec 29, 2015 Update Code Quality Tools to 0.9.0 (#106) Jan 6, 2018
gradlew Better Travis Configuration + dependency updates. (#93) Aug 12, 2017
gradlew.bat Update all deps (#81) Nov 26, 2016


  • Generates boilerplate code for OnActivityResult callbacks and lets you focus on what matters.
  • Generated code is fully traceable and debuggable.
  • Everything is generated during compile time with appropriate errors / warnings.
  • No reflection used!


dependencies {
  compile 'com.vanniktech:onactivityresult:0.7.0'
  annotationProcessor 'com.vanniktech:onactivityresult-compiler:0.7.0'


compile 'com.vanniktech:onactivityresult:0.8.0-SNAPSHOT'
annotationProcessor 'com.vanniktech:onactivityresult-compiler:0.8.0-SNAPSHOT'

Modules are located on Maven Central.


Override onActivityResult in your Activity / Fragment and call ActivityResult.onResult

protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  ActivityResult.onResult(requestCode, resultCode, data).into(this);

Annotate your methods and get the callback

@OnActivityResult(requestCode = 33)
void onActivityResultTestActivity() { /* Do something */ }

@OnActivityResult(requestCode = 1, resultCodes = { Activity.RESULT_OK })
void onActivityResultActivityOk() { /* Only do something when ok */ }

@OnActivityResult(requestCode = 1, resultCodes = { Activity.RESULT_CANCELED })
void onActivityResultActivityCanceled() { /* Only do something when canceled */ }

@OnActivityResult(requestCode = 2)
void onActivityResultPickImage(final int resultCode, final Intent intent) { /* Do something */ }

Various parameters are supported:

  • none
  • int
  • Intent
  • int, Intent
  • Intent, int

Where int parameters will get the resultCode and Intent parameters will get the Intent.

Note: Each annotated method shall only have one int and / or Intent variable.

In addition to that other parameter annotations are supported like:

Some examples can be found here.

The @Extra annotation is generic and works with every type mentioned above. In addition it also supports custom types which are implementing Parcelable or Serializable.

The disadvantage of @Extra is that it won't let you specify a default value therefore the other annotations do exist and should be used when needed.

Advantages over AfterMath

  • Gives you compile error(s) when using invalid RequestCode
  • Annotated method does not require resultCode and Intent to be present. It'll work with every combination (no params, resultCode, Intent, resultCode & Intent, Intent & resultCode). In addition also all custom parameter annotations can be used.
  • The annotations are on mavenCentral and available as a separate artifact
  • More detailed error messages
  • Inheritance support
  • @IntentData annotation with @NonNull & @Nullable support.
  • Specify resultCodes with e.g. resultCodes = { Activity.RESULT_OK }.
  • @Extra, @ExtraBoolean, @ExtraByte, @ExtraChar, @ExtraDouble, @ExtraFloat, @ExtraInt, @ExtraLong, @ExtraShort, @ExtraString annotations.


Thanks to JakeWharton's ButterKnife

Thanks to Hannes Dorfmann's Annotation Processing 101


Copyright (C) 2015 Vanniktech - Niklas Baudy

Licensed under the Apache License, Version 2.0

You can’t perform that action at this time.