excilys/androidannotations — pull request #680 by tbruyelle (2013-07-26 12:34:04+00:00)
Title: Fix IntentBuilder startForResult

Related to #541

In a fragment, startActivityForResult must be called on the `Fragment` and
not on `getActivity()`, or the fragment's onActivityResult will never be
hit.

Now `IntentBuilder` may have up to 3 constructors, depending on the
classpath content. According to the constructor chosen,
`startActivityForResult()` will be called on `Activity`, `android.app.Fragment`
or `android.support.v4.app.Fragment`.

From the AA user perspective, this fix is quite transparent, he just
have to call `MyActivity_.intent(this)`, whatever he's in a fragment or an
activity, it will work.

Generated code in case of both `android.app.Fragment` and `android.support.v4.Fragment` are presents in the classpath (true for current AA junit environment) :

```java
public static ExtendingActivity_.IntentBuilder_ intent(Context context) {
return new ExtendingActivity_.IntentBuilder_(context);
}

public static ExtendingActivity_.IntentBuilder_ intent(android.app.Fragment fragment) {
return new ExtendingActivity_.IntentBuilder_(fragment);
}

public static ExtendingActivity_.IntentBuilder_ intent(android.support.v4.app.Fragment fragment) {
return new ExtendingActivity_.IntentBuilder_(fragment);
}

public static class IntentBuilder_ {

private Context context_;
private final Intent intent_;
private android.app.Fragment fragment_;
private android.support.v4.app.Fragment fragmentSupport_;

public IntentBuilder_(Context context) {
context_ = context;
intent_ = new Intent(context, ExtendingActivity_.class);
}

public IntentBuilder_(android.app.Fragment fragment) {
fragment_ = fragment;
context_ = fragment.getActivity();
intent_ = new Intent(context_, ExtendingActivity_.class);
}

public IntentBuilder_(android.support.v4.app.Fragment fragment) {
fragmentSupport_ = fragment;
context_ = fragment.getActivity();
intent_ = new Intent(context_, ExtendingActivity_.class);
}

public Intent get() {
return intent_;
}

public ExtendingActivity_.IntentBuilder_ flags(int flags) {
intent_.setFlags(flags);
return this;
}

public void start() {
context_.startActivity(intent_);
}

public void startForResult(int requestCode) {
if (fragmentSupport_!= null) {
fragmentSupport_.startActivityForResult(intent_, requestCode);
} else {
if (fragment_!= null) {
fragment_.startActivityForResult(intent_, requestCode);
} else {
if (context_ instanceof Activity) {
((Activity) context_).startActivityForResult(intent_, requestCode);
} else {
context_.startActivity(intent_);
}
}
}
}

}
```

Notes :
- I didn't find proper method to test if a class is present in the classpath. so I used `Class.forName()`. Tell me if I miss something.
- I tested the feature in a test project, and the generated code is well updated according to `android.app.Fragment` or `android.support.v4.app.Fragment` are present or not in the classpath. But I didn't manage to create a junit for that.
- The only tests I wrote are *compile tests* with fragments class which use the new `intent()` methods.