Implementing registerForActivityResult from Fragment to dismiss Activity class

503 views Asked by At

I have a calling like

There is a -> FragmentMain which calls ActivityA -> ActivityA consists of ActivityA_FragmentA -> ActivityA_FragmentA calls ActivityB
-> ActivityB consists of ActivityB_FragmentA , ActivityB_FragmentB , ActivityB_FragmentC

ActivityB_FragmentC is final fragment on whose Ok button , I need to dismiss and return to FragmentMain

Current Implementation: -> On dismiss of ActivityB_FragmentC updates LiveData to which is observed on ActivityB -> This LiveData on ActivityB Used setResult(1000) in ActivityB and then finishActivity -> Using onActivityResult() in ActivityA (result code used to compare 1000) {if result code is 1000 then dismiss ActivityA to show FragmentMain }

How to implement this using . registerForActivityResult() I checked the document for it Doc Link

It says to use Observer and has explanation only for camera fetch .

I needed a simple call where I can pass result code back .. How can I achieve this ?

I want to use registerForActivityResult() in place of onActivityResult() in ActivityA

Trial 1 :

class MyLifecycleObserver(private val registry : ActivityResultRegistry) : DefaultLifecycleObserver { lateinit var getContent : ActivityResultLauncher

override fun onCreate(owner: LifecycleOwner) {
    getContent = registry.register("key", owner, ActivityResultContracts.GetContent()) { uri ->
        // Handle the returned Uri
    }
}

fun selectImage() {
    getContent.launch("image/*")
} }

Am trying to pass result/ call from ActivityA_FragmentA to ActivityA but am able to do it using

ActivityResultContracts.StartActivityForResult()

0

There are 0 answers