« Communication between Modules in Android
Communication between Modules in Android
Today we will show you how we can Communicate or Share Data between Modules in Android.
What is Module or Modular Programming?
We generally use this approach in Android to develop features so we can easily use this module in another app to reuse the same code. If we use the Modular system in Android, we can also use the feature-on-demand built-in feature of Android to minimize our app size.
Communication between Modules:
There are two ways to communicate between Modules in Android:
- Using Callbacks/ Interfaces
- Using Local Broadcast
Using Callbacks/ Interfaces:
Let’s assume that we have a Single Activity application which contains only one fragment i.e. FirstFragment and an activity i.e. MainActivity. The second module contains one fragment i.e. SecondFragment. Let’s start coding and see how we can implement this in Android:
We have a MainActivity that implement’s the SecondModuleNavigation, so we have to override its method. By overriding this method, we can easily share the data from Second Module to the First or Main Module:
1class MainActivity : AppCompatActivity(), SecondModuleNavigation {2 override fun onCreate(savedInstanceState: Bundle?) {3 super.onCreate(savedInstanceState)4 setContentView(R.layout.activity_main)5 }67 override fun shareDataFromSecondModule(data: String) {8 toast(data)9 }10}1112fun Context.toast(message: String) {13 Toast.makeText(this, message, Toast.LENGTH_SHORT).show()14}
SecondFragment, which passes the data to the First or Main Module on button click:
1class SecondFragment : Fragment() {23 private lateinit var binding: FragmentSecondBinding45 override fun onCreateView(6 inflater: LayoutInflater, container: ViewGroup?,7 savedInstanceState: Bundle?8 ): View {9 binding = FragmentSecondBinding.inflate(inflater, container, false)10 return binding.root11 }1213 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {14 super.onViewCreated(view, savedInstanceState)15 setListener()16 }1718 private fun setListener() {19 binding.passData.setOnClickListener {20 (requireActivity() as SecondModuleNavigation).shareDataFromSecondModule("Data From Second Module")21 }22 }23}
Callback or Interface for communication between the two modules:
1interface SecondModuleNavigation {2 fun shareDataFromSecondModule(data: String)3}
Using Local Broadcast:
Another approach is to use the local broadcast, we register the local broadcast on our activity creation and unregister the broadcast on our activity destruction.
MainActivity register’s the local broadcast on its creation and unregister’s the local broadcast on its destruction.
1class MainActivity : AppCompatActivity(), SecondModuleNavigation {2 override fun onCreate(savedInstanceState: Bundle?) {3 super.onCreate(savedInstanceState)4 setContentView(R.layout.activity_main)5 registerBroadcast()6 }78 override fun shareDataFromSecondModule(data: String) {9 toast(data)10 }1112 private fun registerBroadcast() {13 LocalBroadcastManager.getInstance(this)14 .registerReceiver(broadCastReceiver, IntentFilter(BROADCAST_FROM_THIRD_MODULE))15 }1617 private fun unregisterBroadcast() {18 LocalBroadcastManager.getInstance(this)19 .unregisterReceiver(broadCastReceiver)20 }2122 private val broadCastReceiver = object : BroadcastReceiver() {23 override fun onReceive(contxt: Context?, intent: Intent?) {24 when (intent?.action) {25 BROADCAST_FROM_THIRD_MODULE -> {26 toast(intent.getStringExtra(Constants.DATA) ?: "")27 }28 }29 }30 }3132 override fun onDestroy() {33 unregisterBroadcast()34 super.onDestroy()35 }36}3738fun Context.toast(message: String) {39 Toast.makeText(this, message, Toast.LENGTH_SHORT).show()40}4142object Constants {43 const val BROADCAST_FROM_THIRD_MODULE = "data_from_third_module"44}
ThirdFragment, which passes the data to Main Module by using the intent on button click:
1class ThirdFragment: Fragment() {23 private lateinit var binding: FragmentThirdBinding45 override fun onCreateView(6 inflater: LayoutInflater, container: ViewGroup?,7 savedInstanceState: Bundle?8 ): View {9 binding = FragmentThirdBinding.inflate(inflater, container, false)10 return binding.root11 }1213 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {14 super.onViewCreated(view, savedInstanceState)15 setListener()16 }1718 private fun setListener() {19 binding.passData.setOnClickListener {20 val intent = Intent(BROADCAST_FROM_THIRD_MODULE)21 intent.putExtra(DATA, "pass data from third module")22 LocalBroadcastManager.getInstance(requireContext()).sendBroadcast(intent)23 }24 }25}2627object Constants {2829 const val DATA = "data"30 const val BROADCAST_FROM_THIRD_MODULE = "data_from_third_module"31}
For full working code please visit