1

I have 2 projects in Android Studio . I want to launch the activty of one of these projects in an other.

I searched a lot. Some says to create the library project of the project where the activity I want to launch is and then import this library in the other project.

However I don't even know how to create this library project since I want to create it from an existing project.

There is the option 'Import Module' , however i'm afraid it's modifying the whole project.

Help please

Thanks in advance

Bob
  • 111
  • 2
  • 9
  • So are you trying to have two separate apps installed and launch an activity for another app you've developed, or, pull the code in from another project in to the one you're working on? – Ben Pearson Nov 09 '14 at 18:17
  • Yes ! I have two apps installed and one is calling the other when something in my code happens – Bob Nov 09 '14 at 18:28
  • Implicit intent filter and raising that intent would be the correct way. http://developer.android.com/guide/components/intents-filters.html – Ben Pearson Nov 09 '14 at 20:08

2 Answers2

1

This is one way to do it

    Intent i;
    PackageManager manager = getPackageManager();
    try {
        i = manager.getLaunchIntentForPackage("com.exp.yourpackage");
        if (i == null)
            throw new PackageManager.NameNotFoundException();
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        startActivity(i);

This will call the launcher activity of the other application. It is possible to start any activity you want with that method

Michael
  • 815
  • 1
  • 6
  • 21
  • Thanks for the answer ! I haven't test it yet, but i'm afraid that it doesn't recognize my package name since there is no link between the 2 projects. Is your solution suppose that ? – Bob Nov 09 '14 at 18:17
  • if both apps are installed on your phone / your simulator, this will work. The package manager checks if an app is installed with the given package name. If so, you can launch it. If not this code will throw a NameNotFoundException – Michael Nov 09 '14 at 18:19
1

I don't know why but the solution

Intent i;
PackageManager manager = getPackageManager();
try {
    i = manager.getLaunchIntentForPackage("com.exp.yourpackage");
    if (i == null)
        throw new PackageManager.NameNotFoundException();
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);

didn't work for me. Maybe I used it in a wrong way, I don't know.

What works is using

<intent-filter>

in the manifest file of the app we want to trigger and then call the action name of the

<intent-filter>

in the main application.

All details here : http://hmkcode.com/android-start-another-activity-of-another-application/

Bob
  • 111
  • 2
  • 9