0

I have implemented a TabLayout (which uses fragments) in my bottom sheet toolbar that has buttons which should affect the Main Activity. How do I pass the button clicks from the fragments in my TabLayout to the Main Activity?

I'm stuck and I don't know where to start.

gelotinx2
  • 3
  • 2

2 Answers2

0

I think You need to create a function in MainActivity And call that function in the fragment. You can easily access that function bcoz it is your parent activity so you can use it on the button.

References: I know this is in java but I think it's helpful for you link

Hanif Shaikh
  • 500
  • 1
  • 10
0

There are multiple methods to do communication between fragments and its activity . I'll explain the ones which are used widely.

  1. Using an interface.
  2. Using a SharedViewModel for all your fragments and its activity . ( this can be used if you are implementing MVVM architecture )

check this out link

EDIT :

This is a simple step by step implementation on how to pass data from a fragment to activity . I am just using dummy class names and method parameters .

  1. Create a folder called 'listeners' inside your app module , this is where you should have all your interface classes. ( This is just for a clean approach , if that is not your priority then you can save the interface class anywhere ). for Eg I am making TabLayoutFragmentClickListner.
interface TabLayoutFragmentClickListener {
    
}
  1. Add a method to this interface . This is the method which would be called when we click a button inside the fragment. add the required parameters which needs to be passed from fragment to the activity. In this case I am just using a String.
interface TabLayoutFragmentClickListener {
    fun onLayoutFragmentClick(value : String)
}
  1. Implement this interface in the activity in which you want the data to be received.
class MainActivity : AppCompatActivity() , TabLayoutFragmentClickListener {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}
  1. This would make you override the interface method inside that activity.
class MainActivity : AppCompatActivity() , TabLayoutFragmentClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onLayoutFragmentClick(value: String) {
        Log.d("testing" , value)
    }
}
  1. This overiden method is the definition for your interface method in this activity. Hence when you access the interface method from your fragment, the overidden method inside the activity would be called. Try understanding how interface works in java or kotlin.

  2. Now initialise the instance of the listener in your fragment's onAttach method. like this

class TestingFragment : Fragment() {

    lateinit var listener: TabLayoutFragmentClickListener

    override fun onAttach(context: Context) {
        super.onAttach(context)
        listener = context as TabLayoutFragmentClickListener
    }
}
  1. now call the interface method from your fragment with the required parameter. This would hence trigger the interface method definition in your activity hence passing data from the fragment to the activity.
class TestingFragment : Fragment() {

    lateinit var listener: TabLayoutFragmentClickListener

    override fun onAttach(context: Context) {
        super.onAttach(context)
        listener = context as TabLayoutFragmentClickListener
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        test_btn.setOnClickListener {
            listener.onLayoutFragmentClick("testing string")
        }
    }

Hope this helps.

Saneen K P
  • 303
  • 1
  • 8
  • Where do I put the interface? Inside the fragment, main activity, or a new file? – gelotinx2 Dec 02 '22 at 07:23
  • @gelotinx2 I would suggest as a clean approach , create another interface file called something like TabLayoutFragmentListener or something like that according to your use case . Create an instance of that interface in your fragment and call the listener method accordingly. Then implement that interface in your activity. – Saneen K P Dec 02 '22 at 07:28
  • And what should I put inside the interface specifically? thank you for your patience, i'm kinda new to this. – gelotinx2 Dec 02 '22 at 07:44
  • @gelotinx2 Its alright I totally understand , I was also new to those concepts it took me a lot of time to grasp those , I am still learning . So I am always happy to help . I will add another answer with a possible clear explanation. – Saneen K P Dec 02 '22 at 08:20