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

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.

  • 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 7 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 stackoverflow.com/questions/41465053/…. – Hari N Jha Jun 7 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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.