Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

using IntentBuilder and startForResult in Fragment #541

Description

@ligi

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.

Activity

  1. mathieuboniface commented on Mar 26, 2013

    @mathieuboniface
    Contributor

    Hi @ligi,

    Thank you for reporting that issue.

    Which version of AndroidAnnotations are you using ?

    We just provided a fix related to @OnActivityResult on 2.7.1. You can have a look to #381 for more informations.

  2. ligi commented on Mar 27, 2013

    @ligi
    Author

    @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

  3. T12E commented on Jun 6, 2013

    @T12E

    Maybe more explanations are required here.
    A fragment can override the onActivityResult() method, but to make it work, the activity must be started with Fragment.startActivityForResult(). If the activity is started with Activity.startActivityForResult(), then the method is never hit, which is the case when you start an activity with the AA IntentBuilder.

    As @ligi suggested, a modification in the IntentBuilder is required. The constructor should allow a Fragment (the context attribute can be populated with Fragment.getActivity()). If so, startActivityForResult() will be called on the fragment rather than on the activity.

  4. DayS commented on Jun 6, 2013

    @DayS
    Contributor

    To not break the API we should add a new method startForResultFromFragment.
    It would be nicer to handle that in startForResult but some applications will not works anymore with this.

  5. T12E commented on Jun 7, 2013

    @T12E

    It would be nicer to handle that in startForResult but some applications will not works anymore with this.

    Not sure because current usage of IntentBuilder in fragments uses the constructor with the Context, so startForResult() will still call Activity.startActivityForResult(). Only IntentBuilder built with fragment rather than context will use Fragment.startActivityForResult().

  6. tbruyelle commented on Jun 9, 2013

    @tbruyelle
    Contributor

    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 the Fragment class 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 Fragment class 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...

  7. added 2 commits that reference this issue on Jun 9, 2013
    5e983dc
    0011b56
  8. tbruyelle commented on Jun 9, 2013

    @tbruyelle
    Contributor

    About what I started to code, this is now how the IntentBuilder looks like, if both android.app.Fragment and android.support.v4.app.Fragment are present in the classpath :

    I used the OnBackPressedApiDependenciesTest for 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_);
                        }
                    }
                }
            }
    
        }
  9. added 2 commits that reference this issue on Jun 12, 2013
    5c7fbc5
    875c436
  10. DayS commented on Sep 20, 2013

    @DayS
    Contributor

    Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions