1

i want to store a Class in a variable, the purpose is to check if other variable is an instanceOf the class
Here is My Code :

            when (it.itemId) {
                R.id.vend_list -> {
                    replace(R.id.fragmentContainer, vendingList)
                    id = VendListClass
                }
                R.id.label -> {
                    replace(R.id.fragmentContainer, label)
                    id = LabelClass
                }
                R.id.home -> {
                    replace(R.id.fragmentContainer, mainMenu)
                    id = MainMenuClass
                }
                R.id.statistic -> {
                    replace(R.id.fragmentContainer, statistic)
                    id = StatisticClass
                }
                else -> {}
            }

            for(fragment in supportFragmentManager.fragments){
                if(fragment !is id){
                    remove(fragment)
                }
            }
0

3 Answers 3

4

I don't know your exact requirement. But it probably could be designed in other ways, enum? sealed classes? inheritances?

Anyway, straight to your question, hope this helps:

val listCls = List::class
val strCls = String::class

val listObj = listOf(1,2,3)

println("check listObj and listCls: ${listCls.isInstance(listObj)}")
println("check listObj and strCls: ${strCls.isInstance(listObj)}")

output:

check listObj and listCls: true
check listObj and strCls: false
Sign up to request clarification or add additional context in comments.

2 Comments

what is the type of listCls and strCls? I need to store a different Class in one variable using when() conditions. and then i will check if other object is an instance of that Class
@Jooo21 the two *Cls variable is with type KClass you can of course use one variable.
2

You can store a class reference in a KClass<*> variable using ::class or in a Class<*> variable using ::class.java

So based on your original code, this is how you could do it

            // this has to be a nullable type because of your else option
            var id: KClass<*>? = null
    
            when (it.itemId) {
                R.id.vend_list -> {
                    replace(R.id.fragmentContainer, vendingList)
                    id = VendListClass::class
                }
                R.id.label -> {
                    replace(R.id.fragmentContainer, label)
                    id = LabelClass::class
                }
                R.id.home -> {
                    replace(R.id.fragmentContainer, mainMenu)
                    id = MainMenuClass::class
                }
                R.id.statistic -> {
                    replace(R.id.fragmentContainer, statistic)
                    id = StatisticClass::class
                }
                else -> {}
            }

            for(fragment in supportFragmentManager.fragments){
                // if the else option from when has to remove all
                // fragments, just flip the condition to
                // if(id == null || id.isInstance(fragment))
                if(id != null && id.isInstance(fragment)){
                    remove(fragment)
                }
            }

1 Comment

This was my breakthrough: myClass.isInstance(obj). Thanks!
0

Thanks @Kent for helping, but I figured out how to solve this in an ineffective way.

Here is my code:

val obj1 = Object1()
val obj2 = Object2()
val obj3 = Object3()
val obj4 = Object4()

var id : SuperObject? = null

when(certainConditions) {
   option1 -> id = Object1()
   option2 -> id = Object2()
   option3 -> id = Object3()
   option4 -> id = Object4()
}

val otherObject = Object1()

if(id == otherObject) {
    //process logic
}

I'm still looking for the more effective way though.
If anyone found out a better way, please share your answer.

1 Comment

it's called an enum

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.