Intermodule (library projects) communication in android application Intermodule (library projects) communication in android application android android

Intermodule (library projects) communication in android application


What you are looking for is basically a clean approach on how to communicate with other classes. There is not really a difference in whether or not they are in different modules.

The following sample describes how a LoginActivity could navigate to some profile activity. This is just a basic sample to be improved with what you actually need and intend to do!

  1. Define your interfaces

Write interfaces of what you need. Your Login should be able to open a profile page? Well this sounds like it needs a LoginNavigator!

interface LoginNavigator {    void showProfile();}

Include those interfaces in your shared components. There is not really a possibility to go without defining interfaces. You can make them more abstract or more fine grained, this is entirely up to you.

  1. Declare your dependencies

Remember how your Login needs a LoginNavigator? The real problem is on how to supply it to your class. You should have a look at dependency injection, since there are frameworks liks that (could) make this easier. For now, we define an interface for a common component, so that we can retrieve the dependencies we need.

interface NavigatorProvider {    LoginNavigator provideNavigator();}

You may guess it—this method is used to get the actual LoginNavigator that you can use to get the implementation of that interface. Usually you would just declare this dependency in the constructor, but since android is somewhat special you need to get it from somewhere yourself.

  1. Provide your dependencies

The easiest way to go is to just have your application implement this interface (or hold an object that does).

class MyApp extends Application implements NavigatorProvider {    LoginNavigator provideNavigator() {        return new LoginNavigator() {            void showProfile() {                // just some sample code. You should probably not use an                // anonymous class                startActivity(new Intent(this, MyProfileActivity.class));            }        };    }}

Again, you could also return an object that is implementing this interface. This is just a basic sample.

  1. Use the interface. (And don't care about the implementation)

Now the dependency injection is nearly complete. We have an interface that we need, we have some way to provide the dependency, all that's left is to get it and use it.

class LoginActivity extends Activity {    LoginNavigator mNavigator;    void onCreate() {        // get the dependency        mNavigator = ((NavigatorProvider) getApplicationContext()).provideNavigator();        // use it where needed. (again, just sample code)        findShowProfileView().setOnClickListener(new OnClickListener() {            void onClick(View view) {                mNavigator.showProfile();            }        });    }}

Now the dependency is provided, and ready to be used.


What this sample shows is how to basically use interfaces to decouple logic. You will still need some point of entry, since android does not allow to implement your own constructors—this is why the application class is used.


I found that solution using Local Broadcast which is implemented in Application Class and send event on Local Broadcast which is received in Application Class.

class AppApplication : Application() {       override fun onCreate() {        super.onCreate()        registerBroadcast()    }    private fun startProfileActivity() {        val intent = newIntent<MyProfileActivity>(this)        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)        this.startActivity(intent)    }    private fun registerBroadcast() {        LocalBroadcastManager.getInstance(this)          .registerReceiver(broadCastReceiver,IntentFilter(BROADCAST_VIEW_PROFILE))    }    private fun unregisterBroadcast() {        LocalBroadcastManager.getInstance(this)            .unregisterReceiver(broadCastReceiver)    }    private val broadCastReceiver = object : BroadcastReceiver() {        override fun onReceive(contxt: Context?, intent: Intent?) {            when (intent?.action) {                BROADCAST_VIEW_PROFILE -> {                    startProfileActivity()                }            }        }    }    override fun onTerminate() {        super.onTerminate()        unregisterBroadcast()    }} 

When you send the event in an Application like this

private fun viewProfileEventSend() {    // Send Broadcast for view profile to `APP`    val intent = Intent(BROADCAST_VIEW_PROFILE)    LocalBroadcastManager.getInstance(requireContext()).sendBroadcast(intent)}

Because your module doesn't need to get the instance of Application or any interface.