Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference between onCreateView and onViewCreated in Fragment – Kotlin

I know this question has been asked many times but just wanted to have some more clarity.

So i wanted to create a simple fragment with a button that should change the fragment when clicked – very simple and basic one.

hence i create a function, and called it on onCreateView. Nothing happeded.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Then i created onViewCreated after onCreateView and called the same function in it.

It Worked.

My Question is What exactly made it work ?

here is the code

class homeFragment : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    hello()
}
 fun hello()
{
    val button = view?.findViewById<Button>(R.id.button_login)
              button?.setOnClickListener{
                  val action = homeFragmentDirections.actionHomeFragmentToLoginFragment()
                  findNavController().navigate(action)

            Toast.makeText(context,"wao",Toast.LENGTH_LONG).show()
        }
    }

}

>Solution :

As per your comments, you did something like this:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_home, container, false)
    hello() // the method has already returned a View, so this call is never reached.
}

So, when you call hello() in onCreateView after the return statement
the method has no effect because of the return statement.

Since onViewCreated is called after the onCreateView,
the underlying View is no more null in your hello function.

If you still want to use onCreateView, you can do something like this:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val fragmentView = inflater.inflate(R.layout.fragment_home, container, false)
    hello(fragmentView)
    return fragmentView
}

fun hello(buttonHolderView: View?) {
    val button = buttonHolderView?.findViewById<Button>(R.id.button_login)
    button?.setOnClickListener {
        val action = homeFragmentDirections.actionHomeFragmentToLoginFragment()
        findNavController().navigate(action)
        Toast.makeText(context, "wao", Toast.LENGTH_LONG).show()
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading