https://kotlinlang.org logo
#android
Title
# android
s

Satyam G

07/05/2022, 12:54 PM
Is it possible to convert ComponentActivity or Activity to FragmentActivity in Kotlin & HOW ??
😶 5
g

gildor

07/05/2022, 1:53 PM
Why do you need this? ComponentActivity is way to go for all cases
s

Satyam G

07/05/2022, 1:56 PM
There is a use case which requires FragmentActivity or Fragment for initialisation. But in Compose it's giving error if I am directly passing Activity so needs this conversions. Is it possible ??
Need Casting ComponentActivity as Activity or FragmentActivity
g

gildor

07/05/2022, 1:59 PM
ComponentActivity is Activity, no need to cast
n

Nicolas B

07/05/2022, 2:02 PM
Copy code
private fun getContextFromComponent(): Context? {
        return when (val componentCallbacks = componentCallbacksWeakReference.get()) {
            is Fragment -> {
                componentCallbacks.context
            }
            is FragmentActivity -> componentCallbacks
            else -> null
        }
    }
g

gildor

07/05/2022, 2:02 PM
If some API requires FragmentActivity, you just should extend it, it cannot be "casted" somehow
What are you trying to do in this code? What is componentCallvacks?
s

Satyam G

07/05/2022, 2:07 PM
Yes. Agree. But passing Activity as a parameter to my use case which needs Fragment or FragmentActivity as a instance gives me error as this cast can't be succeed
This is the Use case
g

gildor

07/05/2022, 3:08 PM
Right, just extend FragmentActivity, not ComponentActivity
😌 1
s

Satyam G

07/05/2022, 3:10 PM
Checking
5 Views