Is there a method that works like start fragment for result?

后端 未结 10 1299
孤城傲影
孤城傲影 2020-12-02 09:51

I currently have a fragment in an overlay. This is for signing in to the service. In the phone app, each of the steps I want to show in the overlay are their own screens and

10条回答
  •  鱼传尺愫
    2020-12-02 10:06

    Recently, Google has just added a new ability to FragmentManager which made the FragmentManager be able to act as a central store for fragment results. We can pass the data back and forth between Fragments easily.

    Starting fragment.

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Use the Kotlin extension in the fragment-ktx artifact
        setResultListener("requestKey") { key, bundle ->
            // We use a String here, but any type that can be put in a Bundle is supported
            val result = bundle.getString("bundleKey")
            // Do something with the result...
        }
    }
    

    A Fragment that we want the result back.

    button.setOnClickListener {
        val result = "result"
        // Use the Kotlin extension in the fragment-ktx artifact
        setResult("requestKey", bundleOf("bundleKey" to result))
    }
    

    The snippet is taken from Google's official documents. https://developer.android.com/training/basics/fragments/pass-data-between#kotlin

    At the date of this answer written, this feature is still in alpha state. You can try it out using this dependency.

    androidx.fragment:fragment:1.3.0-alpha05
    

提交回复
热议问题