instanceof in kotlin

instanceof in kotlin
December 28, 2018 No Comments Development Pushpendra Kumar



Great you are on the right post! Previously I was working on the Java language for developing an android application. I thought to move with new technology in android development. Then I move completely with Kotlin Language for android application development. But somewhere I have found that some keys and syntax has been changed. One of then instanceof. I was trying to find the solution with the instanceof keyword, but in Kotlin nothing is like that to check instance of. So there is a small question in front of every Android developer. which is

How to check the instanceof anything in android Kotlin

Finally I have fond the solution of this question and I am sharing with you people.

fun checkInstanceOFActivityInFragment() {
        if (activity is MainActivity) {
            //Main Activity
        } else if (activity is OTPActivity) {
            //OTP Activity
        }
    }



fun checkInstanceOfFragment(){
        val f = supportFragmentManager.findFragmentById(R.id.container)
        if (f is FragmentOne) {
            //Fragment One
        } else if (f is FragmentTwo) {
            //Fragment Two
        }
    }

Awesome This is the example code. You can see one more code for checking instances of fragment.

Appart form these cods you can go through with some tutorials which is mention below.

How to use adapter for RecyclerView in Android with Kotlin

How To Work With LayoutInflater In Kotlin

How To Work With Volley In Kotlin



Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *