Solution 1 :
Fragments have just some different lifecycle methods to be aware of.
If you want to use findViewById for instance you can use it inside onViewCreated
.
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById(R.id.your_view_id)
}
Problem :
I made fragments in my Kotlin project and now I wanna fill them. But in the .kt file there’s some codes that I didn’t see in activitys. Can I code that .kt file in fragment like I’m coding an activity, or is it gonna make a problem?
Comments
Comment posted by Rohit Jakhar
There are more boiler-plate code in fragment than activity in android studio but you can remove all those code and can have only one or two method.
Comment posted by alpheratz
But when I try to add findViewById or setContentView it’s giving error. Can’t I use them in fragments?