title: How can I manage fragment and activity considering its creation orders?
body: <h1>What I would like to do</h1>
  
  <p>I would like to call Activity from Fragment to realize both transitions among fragments and activities simultaneously in an Android app.</p>
  
  <p>I have succeeded to call one specific method on activity file from fragment file, but I have trouble with calling an entire activity from a fragment.</p>
  
  <pre><code>SampleFragment - MainActivity's fragmentMethod() is called once  the button is clicked on the page
  Sample1Fragment - FormActivity is called once the button is clicked on the page
  FormActivity - FormActivity2 is called once the button is clicked on the page
  </code></pre>
  
  <p>The above is same as my former question: <a href="https://stackoverflow.com/questions/60619881/how-can-i-fix-the-code-to-call-activity-from-fragment-in-android-app/60624687#60624687">How can I fix the code to call Activity from Fragment in Android App?</a></p>
  
  <h1>Errors and Problems</h1>
  
  <p>It was succeeded to build a project and run the app on the emulator. However, the screen was suddenly shut down when I click the button to call FormActivity on Sample1Fragment.</p>
  
  <p>I have trouble with fix this problem.</p>
  
  <p>Error Message to the below part on Sample1Fragment.kt</p>
  
  <pre><code>Modifier 'override' is not applicable to 'local function'
  </code></pre>
  
  <p>Sample1Fragment.kt</p>
  
  <pre><code>    override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
  
          override fun onViewCreated(view: View, savedInstanceState: Bundle) {
              btnClick2.setOnClickListener(object:View.OnClickListener{
  
                  // here I would like to move to FormActivity
                  override fun onClick(v: View?) {
                      activity?.startActivity(Intent(context, FormActivity::class.java))
                  }
  
              })
          }
  </code></pre>
  
  <h1>Current Code</h1>
  
  <p>MainActivity.kt</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>import androidx.appcompat.app.AppCompatActivity
  import android.os.Bundle
  import android.widget.Toast
  import androidx.fragment.app.Fragment
  import kotlinx.android.synthetic.main.activity_main.*
  
  class MainActivity : AppCompatActivity() {
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          // make the list of fragment
          val fragmentList = arrayListOf&lt;Fragment&gt;(
              SampleFragment(),
              Sample1Fragment()
          )
  
          // create instance of adapter
          val adapter = SamplePagerAdapter(supportFragmentManager, fragmentList)
          /// set adapter
          viewPager.adapter = adapter
      }
  
  
  
      public fun fragmentMethod() {
          Toast.makeText(this@MainActivity, "Method called From Fragment", Toast.LENGTH_LONG).show();
      }
  }
  </code></pre>
  
  <p>SampleFragment.kt</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>import android.os.Bundle
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  import androidx.fragment.app.Fragment
  import kotlinx.android.synthetic.main.fragment_sample.*
  
  class SampleFragment : Fragment() {
  
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          arguments?.let {
  
          }
      }
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_sample, container, false)
      }
  
      override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
          btnClick.setOnClickListener(object:View.OnClickListener{
              override fun onClick(v: View?) {
                  (activity as MainActivity).fragmentMethod()
              }
  
          })
      }
  
  
  
  }
  </code></pre>
  
  <p>Sample1Fragment.kt</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>import android.os.Bundle
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  import androidx.fragment.app.Fragment
  import kotlinx.android.synthetic.main.fragment_sample1.*
  import android.content.Intent
  
  class Sample1Fragment : Fragment() {
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_sample1, container, false)
      }
  
      override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
  
          override fun onViewCreated(view: View, savedInstanceState: Bundle) {
              btnClick2.setOnClickListener(object:View.OnClickListener{
  
                  // here I would like to move to FormActivity
                  override fun onClick(v: View?) {
                      activity?.startActivity(Intent(context, FormActivity::class.java))
                  }
  
              })
          }
  
      }
  
  }
  </code></pre>
  
  <h1>Developping Environment</h1>
  
  <p>Android Studio 3.6.1</p>
  
  <h1>What I tried to do</h1>
  
  <p>It tried to use <code>SingleLiveEvent</code> recommended on an answer, but I haven't found the appropriate way to implement it on my current code, <code>ListViewModel.kt</code> and <code>Sample1Fragment.kt</code>.</p>
  
  <p>Reference: <a href="https://medium.com/androiddevelopers/livedata-with-snackbar-navigation-and-other-events-the-singleliveevent-case-ac2622673150" rel="nofollow noreferrer">LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case)</a></p>
  
  <p>ListViewModel.kt</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>import android.app.usage.UsageEvents
  import android.os.Build
  import androidx.annotation.RequiresApi
  import androidx.lifecycle.LiveData
  import androidx.lifecycle.MutableLiveData
  import androidx.lifecycle.ViewModel
  
  class ListViewModel : ViewModel() {
      private val _navigateToDetails = MutableLiveData&lt;UsageEvents.Event&lt;String&gt;&gt;()
  
      val navigateToDetails : LiveData&lt;UsageEvents.Event&lt;String&gt;&gt;
          get() = _navigateToDetails
  
  
      @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
      fun userClicksOnButton(itemId: String) {
          _navigateToDetails.value =
              UsageEvents.Event(itemId)  // Trigger the event by setting a new Event as a new value
      }
  }
  </code></pre>
  
  <p>These are the error messages I'm facing to</p>
  
  <p><a href="https://i.stack.imgur.com/xmdz0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xmdz0.png" alt="error4"></a>
  <a href="https://i.stack.imgur.com/e5188.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e5188.png" alt="error5"></a></p>
  
  <p>Sample1Fragment.kt</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>import android.os.Bundle
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  import androidx.fragment.app.Fragment
  import kotlinx.android.synthetic.main.fragment_sample1.*
  import android.content.Intent
  
  
  class Sample1Fragment : Fragment() {
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_sample1, container, false)
      }
  
      myViewModel.navigateToDetails.observe(this, Observer {
          it.getContentIfNotHandled()?.let { // Only proceed if the event has never been handled
          fun onViewCreated(view: View, savedInstanceState: Bundle) {
              btnClick2.setOnClickListener(object:View.OnClickListener{
  
                  // here I would like to move to FormActivity
                  override fun onClick(v: View?) {
                      activity?.startActivity(Intent(context, FormActivity::class.java))
                  }
  
              })
          }
      })
  
      }
  
  }
  </code></pre>
  
  <p>This is the error message I'm facing to </p>
  
  <p><a href="https://i.stack.imgur.com/Sarj4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Sarj4.png" alt="Sample1Fragment.kt error"></a></p>