How to initialize context in fragment on Android kotlin

19,935

Solution 1

I changed your codes with below codes :

class HomeRegisteredFragment : Fragment() {

    lateinit var toolbarTile: TextView
    lateinit var handler: Handler

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_home_registered, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //Initialize
        activity?.let {
            toolbarTile = it.findViewById(R.id.homePage_toolbarTitle)
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            //Initialize
            handler = Handler()
            //Set delay
            handler.postDelayed({
                Toast.makeText(requireContext(),"Show",Toast.LENGTH_SHORT).show()
            }, 10)
        }
    }
}

First you should use requireContext() instead of context() for avoid from memory leak.
For show Toast for every time, you can initialize handler in setUserVisibleHint , then after some delay run your code!

I hope help you

Solution 2

Storing context in a variable is a horrible practive and most of the times leads to memory leaks, use requireContext() this method was introduced in Support Library 27.1.0. Nowdays most likely you will have a newer version or even using androidx so there is no excuse for storing a context

Share:
19,935
Jake warton
Author by

Jake warton

Updated on June 17, 2022

Comments

  • Jake warton
    Jake warton about 2 years

    In my application i want show message when fragment has show.
    I used viewPager and BottomNavBar for show 4 fragments!
    I want when click on BottomNavBar items show fragment and i want when visibility fragment show message.
    I write below codes :

    class HomeRegisteredFragment : Fragment() {
    
        lateinit var toolbarTile: TextView
    
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    
            return inflater.inflate(R.layout.fragment_home_registered, container, false)
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            //Initialize
            activity?.let {
                toolbarTile = it.findViewById(R.id.homePage_toolbarTitle)
            }
            //Set title
            toolbarTile.text = resources.getString(R.string.registered)
            context?.let { ContextCompat.getColor(it, R.color.blue_active) }?.let {
                toolbarTile.setTextColor(it)
            }
        }
    
        override fun setUserVisibleHint(isVisibleToUser: Boolean) {
            super.setUserVisibleHint(isVisibleToUser)
            if (isVisibleToUser) {
                Log.e("showFragLog", "Show")
                context?.let { Toast.makeText(it, "Show", Toast.LENGTH_SHORT).show() }
            }
        }
    }
    

    In my above codes, when click on my BottomNavBar for show fragment, show me Log message but not show Toast message.
    When click on another BottomNavBar items and again click on previous BottomNavBar item, then show Toast message.

    I think in first time not initialize context in setUserVisibleHint method.

    How can i initialize context for show Toast in every time?

  • Tony Merritt
    Tony Merritt over 4 years
    This code helped in that I was able to see that I needed to put my code in the on created
  • Mohammad Nouri
    Mohammad Nouri over 4 years
    @TonyMerritt, you can use onCreateView method