【问题标题】:Start app activity from android phone module从 android 手机模块启动应用程序活动
【发布时间】:2020-01-30 16:34:53
【问题描述】:

我的应用中有 2 个模块:app 模块(它是原始模块)和第二个包含活动的模块。

我想在单击一个按钮时启动我的第二个模块中的主要活动,并在单击另一个按钮时返回我的应用程序模块的主要活动。

我尝试执行以下操作,但收到 ClassNotFoundException:

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

还有:

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

还有:

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

但似乎没有任何效果,甚至可能吗? 我该怎么做才能获得这种行为?

【问题讨论】:

  • 虽然我猜你说你从那里尝试过答案......
  • 你能显示第二个模块的清单吗?

标签: android


【解决方案1】:

这样做

    // 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)
   }

【讨论】:

    猜你喜欢
    • 2016-04-25
    • 2013-03-10
    • 2023-03-26
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2016-12-09
    • 2012-06-22
    • 1970-01-01
    相关资源
    最近更新 更多