Is there a method like setResult() in fragment?

前端 未结 4 1093
粉色の甜心
粉色の甜心 2021-02-18 13:02

I am using a fragment. I am getting an error in the onResult() method. I need a substitute method for setResult(RESULT_OK, data) that I can use in my f

相关标签:
4条回答
  • 2021-02-18 13:30

    You should call it on your fragment owning activity:

     getActivity().setResult(Activity.RESULT_OK, data) 
    

    also you might want to finish your activity:

     getActivity().finish();
    
    0 讨论(0)
  • 2021-02-18 13:31

    Use

    getActivity().setResult(Activity.RESULT_OK, data);
    
    0 讨论(0)
  • 2021-02-18 13:45

    If you starting your fragment from another fragment.

    You need to use:

    /**
     * Optional target for this fragment.  This may be used, for example,
     * if this fragment is being started by another, and when done wants to
     * give a result back to the first.  The target set here is retained
     * across instances via {@link FragmentManager#putFragment
     * FragmentManager.putFragment()}.
     *
     * @param fragment The fragment that is the target of this one.
     * @param requestCode Optional request code, for convenience if you
     * are going to call back with {@link #onActivityResult(int, int, Intent)}.
     */
    
    public void setTargetFragment(Fragment fragment, int requestCode) {
    }
    

    When starting your Fragment.

    Like this:

    Fragment newFragment = new YourFragment();
    newFragment .setTargetFragment(this, SOME_REQUEST_INT);
    

    And then, in YourFragment

    Intent data = new Intent();
    data.putExtra(Constants.DATE_KEY, (Date)(getArguments().get(Constants.DATE_KEY)));
    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
    

    Or

    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, null);
    
    0 讨论(0)
  • 2021-02-18 13:48

    Use this it may be help to you..

    getActivity().setResult(Activity.RESULT_OK, data);
    
    0 讨论(0)
提交回复
热议问题