17

Could anyone help me please how can I import setFragmentResult and setFragmentResultListener into my project?

I have implemented 'androidx.fragment:fragment-ktx:1.2.5' but it seems it is not the right package.

Aks4125
  • 4,522
  • 4
  • 32
  • 48
weera
  • 884
  • 1
  • 10
  • 21

3 Answers3

22

First, make sure you have "androidx.fragment:fragment:1.3.0-alpha08" as a dependency. These methods were added in 1.3.0-alpha04, so make sure you have at least that version.

Then, these are the imports:

import androidx.fragment.app.setFragmentResult

and

import androidx.fragment.app.setFragmentResultListener
Victoria Gonda
  • 256
  • 2
  • 4
  • Nice, btw the last version "1.3.0-rc01" at doc page (it is still a candidate release state): https://developer.android.com/jetpack/androidx/releases/fragment – MohammadL Dec 27 '20 at 16:34
11

If you are using java please try this code below :

setFragmentResult :

requireActivity().getSupportFragmentManager().setFragmentResult("request_Key", new Bundle());

setFragmentResultListener :

requireActivity().getSupportFragmentManager().setFragmentResultListener("request Key", getViewLifecycleOwner(), new FragmentResultListener() {
            @Override
            public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {

            }
        });

dependency use :

implementation "androidx.fragment:fragment:1.3.0"
Hor Chanpheng
  • 811
  • 8
  • 9
  • The above was helpful but I found I needed to use getParentFragmentManager() instead of requireActivity().getSupportFragmentManager() – Martin Aug 09 '21 at 13:34
  • @Martin : I saw getParentFragmentManager() too but getSupportFragmentManager() was working for me. ^^ – Hor Chanpheng Aug 10 '21 at 01:30
  • This is supposed to be the equivalent of startActivityForResult, right? If so, what about initiating it? And shouldn't having this "request_Key" need to be unique between all Fragments that the Activity ever has to deal with? – android developer Dec 01 '21 at 22:57
8

As of Feb'21, 1.3.0 is now available. Source

implementation 'androidx.fragment:fragment-ktx:1.3.0'
debugImplementation 'androidx.fragment:fragment-testing:1.3.0'

This should resolve the reference.

Aks4125
  • 4,522
  • 4
  • 32
  • 48