You are on page 1of 1

What is clickListener you might be wondering?

This is another interface you will


create shortly. Its job is to pass the list from the Fragment to the Activity. Where
finally you will be able to pass the list to your second Activity.

At the top of MainFragment.kt, add the following interface:

interface MainFragmentInteractionListener {
fun listItemTapped(list: TaskList)
}

In the class declaration, update the constructor to allow passing in a


MainFragmentInteractionListener.

class MainFragment(val clickListener:


MainFragmentInteractionListener) : Fragment(),
ListSelectionRecyclerViewAdapter.ListSelectionRecyclerViewClickL
istener {

In the companion object, update the newInstance() method to allow the Fragment
to setup itself up with the listener passed in.

The Fragment is setup. Now it's time to set the MainActivity up. Open
MainActivity.kt, then update the class declaration to implement the
MainFragmentInteractionListener interface.

class MainActivity : AppCompatActivity(),


MainFragment.MainFragmentInteractionListener {

Next, update the creation of MainFragment in onCreate() so it passes in the


Activity. This allows the Activity to receive the task list tapped by the user.

if (savedInstanceState == null) {
val mainFragment = MainFragment.newInstance(this)
supportFragmentManager.beginTransaction()
.replace(R.id.container, mainFragment)
.commitNow()
}

You might also like