Skip to content

Instantly share code, notes, and snippets.

@jquerius
Last active May 3, 2021 22:14
Show Gist options
  • Save jquerius/40aa61b2ff4d0b1ae267212d7dd965e5 to your computer and use it in GitHub Desktop.
Save jquerius/40aa61b2ff4d0b1ae267212d7dd965e5 to your computer and use it in GitHub Desktop.

Revisions

  1. jquerius revised this gist Apr 15, 2018. 1 changed file with 6 additions and 10 deletions.
    16 changes: 6 additions & 10 deletions ActivityFragmentExample.kt
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,15 @@
    class ExampleFragment : Fragment() {

    // this is the instance of our parent activity's interface that we define here
    private var mListener: OnFragmentInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    }

    fun onButtonPressed(uri: Uri) {
    if (mListener != null) {
    mListener!!.onFragmentInteraction(uri)
    }
    }

    override fun onAttach(context: Context?) {
    super.onAttach(context)
    if (context is OnFragmentInteractionListener) {
    @@ -21,29 +18,28 @@ class ExampleFragment : Fragment() {
    throw RuntimeException(context!!.toString() + " must implement OnFragmentInteractionListener")
    }
    }

    override fun onDetach() {
    super.onDetach()
    mListener = null
    }

    /**
    * Here we define the methods that we can fire off
    * in our parent Activity once something has changed
    * within the fragment.
    */
    interface OnFragmentInteractionListener {
    fun onFragmentInteraction(uri: Uri)
    }

    }

    class AddVehicleActivity : AddVehicleStep1.OnFragmentInteractionListener, AppCompatActivity() {

    // this is the callback-like function that will run when the fragment
    // tells it to
    override fun onFragmentInteraction(uri: Uri) {
    // save some data from the fragment...
    // other business logic...
    }

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    }

    }
  2. jquerius revised this gist Apr 15, 2018. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion ActivityFragmentExample.kt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    class ExampleFragment : Fragment() {

    // this is the instance of our parent activity's interface that we define here
    private var mListener: OnFragmentInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
    @@ -33,7 +34,9 @@ class ExampleFragment : Fragment() {
    }

    class AddVehicleActivity : AddVehicleStep1.OnFragmentInteractionListener, AppCompatActivity() {


    // this is the callback-like function that will run when the fragment
    // tells it to
    override fun onFragmentInteraction(uri: Uri) {
    // save some data from the fragment...
    // other business logic...
  3. jquerius created this gist Apr 15, 2018.
    46 changes: 46 additions & 0 deletions ActivityFragmentExample.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    class ExampleFragment : Fragment() {

    private var mListener: OnFragmentInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    }

    fun onButtonPressed(uri: Uri) {
    if (mListener != null) {
    mListener!!.onFragmentInteraction(uri)
    }
    }

    override fun onAttach(context: Context?) {
    super.onAttach(context)
    if (context is OnFragmentInteractionListener) {
    mListener = context
    } else {
    throw RuntimeException(context!!.toString() + " must implement OnFragmentInteractionListener")
    }
    }

    override fun onDetach() {
    super.onDetach()
    mListener = null
    }

    interface OnFragmentInteractionListener {
    fun onFragmentInteraction(uri: Uri)
    }

    }

    class AddVehicleActivity : AddVehicleStep1.OnFragmentInteractionListener, AppCompatActivity() {

    override fun onFragmentInteraction(uri: Uri) {
    // save some data from the fragment...
    // other business logic...
    }

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    }

    }