从另一个模块打开活动导致循环依赖。

huangapple go评论67阅读模式
英文:

Open Activity From Another Module Giving Circular Dependency

问题

我正在开发一个 Android 应用程序,需要调用位于主模块中的 Activity。我面临的问题是,包含一个 Fragment 的子模块无法访问主模块中的 Activity。
我刚刚查看了以下链接以寻求帮助,但无法解决访问主模块活动的问题。

以下是我从中调用父模块的 Fragment 类:

Intent intent = new Intent(getActivity(), QRScannedResultActivity.class);
intent.putExtra("sendedscannedcontent", resultString);
startActivity(intent);

从调用主模块的函数的图像如下所示:

(图像链接已省略)

模块层次结构如下图所示:

(图像链接已省略)

在 Gradle 中添加 implementation project(path:':app') 后,出现循环依赖错误:

(图像链接已省略)

英文:

I am working on Android app that has to call to Activity which lies in the Main Module. The Issue I am facing is, the Sub Module which contains a Fragment is unable to Access to Activity in the Main Module.
I have Just seen these links for the Help but the Problem of Accessing to main module activity could not resolved..

Here is the Fragment Class in the Module from where I am calling to Parent Module..

        Intent intent = new Intent(getActivity(), QRScannedResultActivity.class);

        intent.putExtra("sendedscannedcontent", resultString);
        startActivity(intent);

the Function Image from where I am Calling to main
从另一个模块打开活动导致循环依赖。

The Module Hirarchy looks like

从另一个模块打开活动导致循环依赖。

After Adding implementation project(path:':app') in Gradle it Gives the Circular Dependency Error

从另一个模块打开活动导致循环依赖。

答案1

得分: 1

你可以使用 Class.forName() 获取 QRScannedResultActivity 类的引用。

假设 QRScannedResultActivity 位于 com.maximus.technologies.view 包中,如果不是这种情况,请替换包路径。

try {
    Intent intent = new Intent(getActivity(), 
    Class.forName("com.maximus.technologies.views.QRScannedResultActivity"));
    intent.putExtra("sendedscannedcontent", resultString);
    startActivity(intent);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}
英文:

You can use Class.forName() to get QRScannedResultActivity class reference

Assuming that QRScannedResultActivity is in com.maximus.technologies.view package, replace the package path if that's not the case

try {
    Intent intent = new Intent(getActivity(), 
    Class.forName("com.maximus.technologies.views.QRScannedResultActivity"));
    intent.putExtra("sendedscannedcontent", resultString);
    startActivity(intent);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

huangapple
  • 本文由 发表于 2020年10月21日 14:56:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64458172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定