One of the more painful pieces of the Android lifecycle occurs inside onActivityResult. The more DialogFragment and startActivityForResult calls you have, the more it inevitably descends into a mess of switch statements and spaghetti.

A more reasonable approach is to encapsulate each piece of callback behavior into its own handler method. That is precisely what Velodrome does.

public static final int REQUEST_DELETE = 0;
public static final int REQUEST_TEXT_ENTRY = 1;

@OnActivityResult(REQUEST_DELETE)
public void onConfirmDelete() {
    // Burn the world.
}

@OnActivityResult(REQUEST_TEXT_ENTRY)
public void onTextResult(@Arg("text") String text) {
    // 'text' is automatically extracted from the data Intent.
}

@OnActivityResult(value=REQUEST_TEXT_ENTRY, resultCode=Activity.RESULT_CANCELED)
public void onTextAborted(Intent data) {
    // Velodrome only fires on RESULT_OK by default. 
    // You can override that behavior in the annotation args. 
    // 'data' is the original data Intent passed to onActivityResult.
}

@OnActivityResult({100, 101})
public void onOther() {
    // You can even declare a single handler for multiple request codes, if that's your thing.
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Velodrome.handleResult(this, requestCode, resultCode, data);
}

Performance

Velodrome uses reflection to resolve handler methods. This means it isn't blazing fast. That being said, we don't believe that the performance hit is substantial enough to warrant the pain of dealing with annotation processing.

However, we wouldn't be sad if some intrepid soul were to submit a pull request!

Install

dependencies {
    compile 'com.levelmoney.velodrome:velodrome:1.0'
}

Proguard

-keepclassmembers class * { @com.levelmoney.velodrome.annotations.OnActivityResult *; }

License

Copyright 2015 Level Money, Inc.

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.