Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

759
Views
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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report

0

If you are looking for application context to show the toast message, try the below way and see if it works. Also, initialize it onCreate method so you have the activity context at that point.

val appContext = context!!.applicationContext
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!