Repository navigation
using IntentBuilder and startForResult in Fragment #541
Description
Activity
@mathieuboniface thanks for the quick reply & yes the problem is still there with 2.7.1 ( just tested ) was using 2.7 before
the problem is this generated code:public void startForResult(int requestCode) { if (context_ instanceof Activity) { ((Activity) context_).startActivityForResult(intent_, requestCode); } else { context_.startActivity(intent_); } }there should be a way to do startActivityForResult on the Fragment and not on the Activity for encapsulation
Maybe more explanations are required here.
A fragment can override theonActivityResult()method, but to make it work, the activity must be started withFragment.startActivityForResult(). If the activity is started withActivity.startActivityForResult(), then the method is never hit, which is the case when you start an activity with the AAIntentBuilder.As @ligi suggested, a modification in the
IntentBuilderis required. The constructor should allow aFragment(the context attribute can be populated withFragment.getActivity()). If so,startActivityForResult()will be called on the fragment rather than on the activity.To not break the API we should add a new method
startForResultFromFragment.
It would be nicer to handle that instartForResultbut some applications will not works anymore with this.It would be nicer to handle that in startForResult but some applications will not works anymore with this.
Not sure because current usage of
IntentBuilderin fragments uses the constructor with the Context, sostartForResult()will still callActivity.startActivityForResult(). OnlyIntentBuilderbuilt with fragment rather than context will useFragment.startActivityForResult().I'm currently trying to implement a fix for this issue, by adding a new constructor for the
IntentBuilder.
But I'm facing a problem, the need of import theFragmentclass in the extended activity, which causes error on projects built under sdk<11 and which doesn't ship the support library.How do you handle such things AA guys?
I could test if the
Fragmentclass is present in the classpath (first the support version, then the standard version) and enable the feature only in that case. But I wonder how to write a test that ensure such feature. It would need 2 versions of the sdks, plus the support library...- added 2 commits that reference this issue
on Jun 9, 2013 About what I started to code, this is now how the
IntentBuilderlooks like, if bothandroid.app.Fragmentandandroid.support.v4.app.Fragmentare present in the classpath :I used the
OnBackPressedApiDependenciesTestfor my test, but still looking for a solution to write a real test on this feature.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, ActivityWithBackgroundMethod_.class); } public IntentBuilder_(android.app.Fragment fragment) { fragment_ = fragment; context_ = fragment.getActivity(); intent_ = new Intent(context_, ActivityWithBackgroundMethod_.class); } public IntentBuilder_(android.support.v4.app.Fragment fragment) { fragmentSupport_ = fragment; context_ = fragment.getActivity(); intent_ = new Intent(context_, ActivityWithBackgroundMethod_.class); } public Intent get() { return intent_; } public ActivityWithBackgroundMethod_.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_); } } } } }
- added 2 commits that reference this issue
on Jun 12, 2013 Merged.
when using the IntentBuilder and startForResult in a Fragment the startAcitivityForResult is called on the activity and not the Fragment. That results in onActivityResult is called on Result for the Activity and not the Fragment. Not sure how to solve this as the IntentBuilder does not have the reference to the calling fragment - perhaps the startForResult should have a target parameter - or you can give a fragment to .intent() instead of only a activity.