0

I have 2 modules in my app: app module (which is the original module) and a second module which hold an activity.

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. Project architecture

I've tried to do the following but got a 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 ?

Liam
  • 469
  • 1
  • 6
  • 16

1 Answers1

1

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)
   }
VVB
  • 7,363
  • 7
  • 49
  • 83