Android片段结果监听器





在Android上,片段之间的数据传输可以通过不同的方式完成:使用ViewModel或Fragments API通过父Activity进行传输Fragment Target API最近已收到不推荐使用的状态,Google建议改为使用Fragment结果API



什么是片段结果API?这是Google的新工具,可让您使用密钥在片段之间传输数据。为此,使用FragmentManager,后者又实现FragmentResultOwner接口。FragmentResultOwner充当我们在片段之间传递的数据的中央存储库。



怎么运行的?



图片

如上所述,我们的FragmentManager实现了FragmentResultOwner接口,该接口存储了ConcurrentHashMap<String, Bundle>该HashMap通过字符串键存储我们的Bundles。一旦一个片段被签名(或已经签名),它就会收到相同密钥的结果。



重要的是要知道



  • - setResultFragmentListener() , setFragmentResult(),
  • “Key + Result (Bundle)“ 1
  • STARTED
  • DESTROYED ResultListener


?





:



FragmentManager.setFragmentResult(key: String, bundle: Bundle)


, Bundle. Bundle .



Kotlin



button.setOnClickListener {
    val result = "result"
    //     Kotlin    fragment-ktx
    setFragmentResult("requestKey", bundleOf("bundleKey" to result))
}


Java



button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Bundle result = new Bundle();
        result.putString("bundleKey", "result");
        getParentFragmentManager().setFragmentResult("requestKey", result);
    }
});




FragmentManager FragmentResultListener . FragmentManager.setFragmentResult()



Kotlin



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
     //     Kotlin  
    setFragmentResultListener("requestKey") { key, bundle ->
        //     ,  Bundle-
        val result = bundle.getString("bundleKey")
    }
}


Java



@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getParentFragmentManager().setFragmentResultListener("key", this, new FragmentResultListener() {
        @Override
        public void onFragmentResult(@NonNull String key, @NonNull Bundle bundle) {
            String result = bundle.getString("bundleKey");
        }
    });
}


2 : key: String bundle: Bundle.

— , . — Bundle, .



Parent Fragment Manger



图片


FragmentManager- :



  • FragmentManager ( Activity), FragmentManager, Activity
  • , childFragmentManager ( )


, FragmentResultListener FragmentManager-.





/ FragmentResultListener, FragmentScenario API, .





, FragmentManager? , FragmentResultListener :



@Test
fun testFragmentResult() {
    val scenario = launchFragmentInContainer<ResultFragment>()
    lateinit var actualResult: String?
    scenario.onFragment { fragment ->
        fragment.parentFragmentManagager.setResultListener("requestKey") { key, bundle ->
            actualResult = bundle.getString("bundleKey")
        }
    }
    onView(withId(R.id.result_button)).perform(click())
    assertThat(actualResult).isEqualTo("result")
}

class ResultFragment : Fragment(R.layout.fragment_result) {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        view.findViewById(R.id.result_button).setOnClickListener {
            val result = "result"
            setResult("requestKey", bundleOf("bundleKey" to result))
        }
    }
}




, FragmentManager. FragmentResultListener .



@Test
fun testFragmentResultListener() {
    val scenario = launchFragmentInContainer<ResultListenerFragment>()
    scenario.onFragment { fragment ->
        val expectedResult = "result"
        fragment.parentFragmentManagager.setResult("requestKey", bundleOf("bundleKey" to expectedResult))
        assertThat(fragment.result).isEqualTo(expectedResult)
    }
}

class ResultListenerFragment : Fragment() {
    var result : String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setResultListener("requestKey") { key, bundle ->
            result = bundle.getString("bundleKey")
        }
    }
}




FragmentResultListener ,  Google. , , , . , , , , .



为了能够使用FragmentResultListener,我们需要在依赖项中包含片段1.3.0-alpha04或更高版本的版本



  • Java版本:androidx.fragment:片段:1.3.0-alpha04
  • Kotlin版本:androidx.fragment:片段-ktx:1.3.0-alpha04
  • 测试:androidx.fragment:片段测试:1.3.0-alpha04



All Articles