Search code examples
androidandroid-fragmentskotlinandroid-context

How to initialize context in fragment on Android kotlin


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?


Solution

  • 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