简体   繁体   English

从 android 手机模块启动应用程序活动

[英]Start app activity from android phone module

I have 2 modules in my app: app module (which is the original module) and a second module which hold an activity.我的应用程序中有 2 个模块:app 模块(它是原始模块)和一个包含活动的第二个模块。

I want to launch the main activity which is in my second module when clicking on a button and get back to my app module's main activity when clicking on another button.我想在单击按钮时启动位于我的第二个模块中的主要活动,并在单击另一个按钮时返回到我的应用程序模块的主要活动。 项目架构

I've tried to do the following but got a ClassNotFoundException:我尝试执行以下操作,但遇到 ClassNotFoundException:

packageManager.getLaunchIntentForPackage("com.example.secondmodule")?.let {
    startActivity(intent)
}

and that:然后:

val intent = Intent(this, Class.forName("com.example.secondmodule.MainActivity"))
        startActivity(intent)

and that:然后:

startActivity(Intent("com.example.secondmodule"))

but nothing seems to work, is it even possible ?但似乎没有任何效果,甚至可能吗? What can I do to obtain this behavior ?我该怎么做才能获得这种行为?

Do it this way这样做

    // Get an instance of PackageManager
    val pm = applicationContext.packageManager

    // Initialize a new Intent
    val intent:Intent? = pm.getLaunchIntentForPackage(packageName)

    // Add category to intent
    intent?.addCategory(Intent.CATEGORY_LAUNCHER)

   // If intent is not null then launch the app
   if(intent != null) {
     applicationContext.startActivity(intent)
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM