« Communication between Modules in Android

module_communication.png

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:

  1. Using Callbacks/ Interfaces
  2. 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 }
6
7 override fun shareDataFromSecondModule(data: String) {
8 toast(data)
9 }
10}
11
12fun 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() {
2
3 private lateinit var binding: FragmentSecondBinding
4
5 override fun onCreateView(
6 inflater: LayoutInflater, container: ViewGroup?,
7 savedInstanceState: Bundle?
8 ): View {
9 binding = FragmentSecondBinding.inflate(inflater, container, false)
10 return binding.root
11 }
12
13 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
14 super.onViewCreated(view, savedInstanceState)
15 setListener()
16 }
17
18 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 }
7
8 override fun shareDataFromSecondModule(data: String) {
9 toast(data)
10 }
11
12 private fun registerBroadcast() {
13 LocalBroadcastManager.getInstance(this)
14 .registerReceiver(broadCastReceiver, IntentFilter(BROADCAST_FROM_THIRD_MODULE))
15 }
16
17 private fun unregisterBroadcast() {
18 LocalBroadcastManager.getInstance(this)
19 .unregisterReceiver(broadCastReceiver)
20 }
21
22 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 }
31
32 override fun onDestroy() {
33 unregisterBroadcast()
34 super.onDestroy()
35 }
36}
37
38fun Context.toast(message: String) {
39 Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
40}
41
42object 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() {
2
3 private lateinit var binding: FragmentThirdBinding
4
5 override fun onCreateView(
6 inflater: LayoutInflater, container: ViewGroup?,
7 savedInstanceState: Bundle?
8 ): View {
9 binding = FragmentThirdBinding.inflate(inflater, container, false)
10 return binding.root
11 }
12
13 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
14 super.onViewCreated(view, savedInstanceState)
15 setListener()
16 }
17
18 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}
26
27object Constants {
28
29 const val DATA = "data"
30 const val BROADCAST_FROM_THIRD_MODULE = "data_from_third_module"
31}

For full working code please visit

https://github.com/anishakd4/ModuleCommunication