Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intermodule (library projects) communication in android application

Tags:

In the below shown diagram, I am having 3 modules(as android library) which extends the base "common components module" and all this 3 modules will be added to a single android application. All 3 modules are independent modules but when it comes as an application, it would require to share some data, launch other module and requires more inter-communication.

So can anyone let me know how we can implement the "Data Sharing Layer" and "Navigation Controller" in this kind of architecture?

Example: Module1 -> Login, Module2 -> Profile Management etc and there could be "n" number of modules based on the application need.

enter image description here

like image 800
Dinash Avatar asked Mar 01 '16 13:03

Dinash


People also ask

How do you communicate between modules in Android?

There are two ways to communicate between Modules in Android: Using Callbacks/ Interfaces. Using Local Broadcast.

What is multi-module project in Android?

A project with multiple Gradle modules is known as a multi-module project. This guide encompasses best practices and recommended patterns for developing multi-module Android apps. Note: This page assumes a basic familiarity with the recommended app architecture.

What is Library in Android app?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.

Does Android library have application class?

The Application class in Android is the base class within an Android app that contains all other components such as activities and services. The Application class, or any subclass of the Application class, is instantiated before any other class when the process for your application/package is created.


1 Answers

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 dagger-2 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.

like image 163
David Medenjak Avatar answered Oct 01 '22 02:10

David Medenjak