Android应用程序中的模块间(库项目)通信 [英] Intermodule (library projects) communication in android application

查看:497
本文介绍了Android应用程序中的模块间(库项目)通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面显示的图中,我有3个模块(作为android库),该模块扩展了基本的公共组件模块",所有这3个模块都将添加到单个android应用程序中.这3个模块都是独立的模块,但是当作为应用程序使用时,将需要共享一些数据,启动其他模块,并且需要更多的相互通信.

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?

示例:模块1->登录名,模块2->概要文件管理等,根据应用程序的需要,模块数量可能为n.

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

推荐答案

您所寻找的基本上是一种与其他类进行通讯的干净方法.它们是否在不同的模块中并没有真正的区别.

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.

以下示例描述了LoginActivity如何导航到某些配置文件活动. 这只是一个基本示例,可以根据您的实际需求和打算进行改进!

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. 定义您的界面

编写您需要的界面.您的登录名应该可以打开个人资料页面?听起来好像需要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. 声明您的依赖项

还记得您的登录名如何需要LoginNavigator吗?真正的问题在于如何将其提供给您的班级.您应该看看依赖项注入,因为有框架喜欢(可以)使此操作更容易.现在,我们为通用组件定义一个接口,以便我们可以检索所需的依赖项.

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();
}

您可能会猜到—此方法用于获取实际的LoginNavigator,您可以使用该LoginNavigator来获取该接口的实现.通常,您只需要在构造函数中声明此依赖关系,但是由于android有点特殊,因此您需要自己从某个地方获取它.

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. 提供依赖项

最简单的方法是让您的应用程序实现此接口(或保存一个有此功能的对象).

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. 使用界面. (并且不在乎实现)

现在,依赖项注入已接近完成.我们有一个需要的接口,我们有某种方式来提供依赖关系,剩下的就是获取和使用它.

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.

此样本显示的是基本上如何使用接口来解耦逻辑.您仍然需要一些入口点,因为android不允许实现自己的构造函数—这就是为什么要使用应用程序类的原因.

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.

这篇关于Android应用程序中的模块间(库项目)通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆