我如何从另一个功能模块访问活动 [英] How can i access an activity from another feature module

查看:64
本文介绍了我如何从另一个功能模块访问活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个即时应用程序,其中包括应用程序模块,基本功能模块,即时应用程序模块和另一个功能模块.问题是我无法从基本功能和功能模块访问应用程序模块的活动,而在基本功能模块和功能模块之间访问应用程序模块的活动,但是我能够从应用程序模块访问基本功能模块的活动.

I am creating an instant app, which include application module, base feature module, instant app module and an another feature module. Problem is i am not able to access the activities of application module from base-feature and feature module and same between base-feature module and feature module but i am able to access activity of base-feature module from application module.

现在我正在使用:

Intent i = new Intent(this,
                    Class.forName("com.demo.test.appmodule.TextActivity"));

通过这种方法,studio不会在编译时向我显示任何错误.

by this method studio don't show me any errors at compile time.

  1. 两个不同功能模块之间是否还有其他通信方式?
  2. 为什么我能够从应用程序模块访问基本功能模块的活动,反之亦然?
  3. 我们可以从基本模块或任何其他功能模块访问应用程序模块的活动吗?

我可以使用一个链接来定义即时应用程序的项目结构

Can i have a link which define the project structure for an instant app

预先感谢

推荐答案

之所以不能在要素之间直接通信是因为它们彼此独立.

The reason why you can't communicate directly between features is because they're independent from one another.

处理此问题的正确方法是使用其URL进行调用,例如:

The correct way to handle this is calling it with its URL, example: android-instant-apps/hello-feature-module/HelloActivity.java

Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://hello-feature.instantappsample.com/goodbye"));
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(intent);

在即时应用程序结构中,库充当功能模块的库,功能内置在 APKs 中.在已安装的应用程序结构中,基础和功能均充当应用程序模块的库.一些解释可以在这里找到:

In an instant-app structure, the base acts as a library for the feature modules, and the features are built into APKs. In an installed-app structure, both the base and features act as libraries for the application module. Some explanation can be found here:

Android:您可以将活动添加到Instant App模块中吗?

过去有一个页面@ https://g.co/instantapps 解释了即时应用,但看起来好像丢失了.但是,您可以看一下:

There used to be a page @ https://g.co/instantapps that explained the structure of instant apps, but looks like it's missing. However, you can take a look at:

不,您将无法直接从功能访问应用程序的活动.作为已安装的应用程序, com.android.feature 模块被编译为/表现为 com.android.library 模块,因此在此处应用相同的规则:应用程序依赖于库,而不是相反.要遍历该方向,您将需要使用与上面所示相同的Intent.

And no, you won't be able to directly access activities of the application from a feature. As an installed-app, com.android.feature modules are compiled/behave as com.android.library modules, so apply the same rules here: the application depends on the library, not the other way around. To traverse that direction, you will need to use the same kind of Intent as shown above.

com.android.application 中的所有内容都将与Instant-app的功能模块隔离,并且只会显示在已安装的应用程序中.

Anything in com.android.application will be isolated from the feature modules of the instant-app, and will only appear in the installed-app.

这篇关于我如何从另一个功能模块访问活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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