Intermodule (library projects) communication in android application

僤鯓⒐⒋嵵緔 提交于 2019-12-03 11:39:55

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/35723666/intermodule-library-projects-communication-in-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!