0

I have some Android Modules which all depend on the same Library Module, for business logic.

com.example.api
-> com.example.app1
-> com.example.app2
-> com.example.app3

Now, I try to get the current MainActivity (android.intent.action.MAIN) from the library. Without specifying the wanted activity while calling the action, I get the IntentPicker for all the devices Apps, which have the MAIN action in it.

How do I get the classpath of my current MainActivity? i.e. com.example.app1.MainActivity

Zoe
  • 27,060
  • 21
  • 118
  • 148
Pascal Syma
  • 729
  • 1
  • 6
  • 19

2 Answers2

0

As Module a separate section for the Main Application and a module contains basically library classes and resources for the application. A module can be created and tested separately. When you use a module withing a project then your main application can use the resources of the module but your module cannot use resource of the Main Application.

To fullfil your need you can call main activity from the module using package manager. You can see the detail on developer site. The call be something like

Intent appMainIntent = getPackageManager().getLaunchIntentForPackage("com.app.myproject");
startActivity(appMainIntent);

Just to inform you, appMainIntent can be null if the package is not found. So do check null pointer exception in your code.

Hari N Jha
  • 484
  • 1
  • 3
  • 11
  • So I have to hardcode all known packages to test, which app it is? There is no way to find the current main activity? – Pascal Syma Jun 07 '19 at 10:47
  • Yes. You have to call each time with the package name and have to check null occurance. for the ease of call you can follow the link https://stackoverflow.com/questions/41465053/how-to-call-activity-from-a-library-module-in-android-studio. – Hari N Jha Jun 07 '19 at 10:55
0

To get the current package name of the application, you can call getApplicationContext().getPackageName() and to get the launch intent for this package, as @hari-n-jha mentioned, you can call getPackageManager().getLaunchIntentForPackage( String packageName ).

The full solution would be:

String packageName = getApplicationContext().getPackageName();
Intent i = getPackageManager().getLaunchIntentForPackage(packageName);
startActivity(i);
Pascal Syma
  • 729
  • 1
  • 6
  • 19
  • When getting packageName I get this error: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference – Boris Gafurov Jul 25 '23 at 16:07