title: What is the guaranteed way to get not null Context in Fragment?
body: <p>I saw many examples for how to get the Context in BaseFragment like this:</p>
  
  <pre><code>protected lateinit var ctx: Context
  
  override fun onAttach(context: Context?) {
      super.onAttach(context)
      ctx = context!!
  }
  </code></pre>
  
  <p>And then we can use the context instance in our other fragments that extend BaseFragment. <a href="https://stackoverflow.com/a/30641537/6367262">One</a>, <a href="https://stackoverflow.com/a/24256683/6367262">two</a>, <a href="https://stackoverflow.com/a/53580994/6367262">three (it's offered to get Context from onViewCreated())</a> and so on. <strong>It looks like workaround.</strong></p>
  
  <p>I also had some cases when "fragment detached from Activity" exceptions occurred while using getActivity() or getContext() in Fragment.</p>
  
  <p>So, what's the true way?</p>