Search code examples
androidandroid-intentlaunch

How can I launch 2 applications from current simultaneously (one in background)?


I want to launch 2 Android applications from the current application. One of them should be launched in background and the other one in foreground with which user will interact with now. When I try the existing solutions for launching multiple intents (like setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)andtry....finally), they don't work, probably because when 1 application is launched device switches to that instantly; and these solutions are for intents launching multiple activities in the same applications. Please suggest a solution other than launching one as a service.

UPDATE - Answer:

Based on David Wasser's answer, this works:

    final Intent intent1=getPackageManager().getLaunchIntentForPackage("example.app1.package");
    final Intent intent2=getPackageManager().getLaunchIntentForPackage("example.app3.package");
    Handler mHandler=new Handler();
    Runnable mLaunchTask = new Runnable() {
             public void run() {
                      startActivity(intent1);
             }
    };
    mHandler.postDelayed(mLaunchTask,1000);
    startActivity(intent2);

Solution

  • There are a few ways to do this. Here are 2:

    1) Launch App B. Start a background thread or post a Runnable to a Handler that will launch App C after a few seconds. You need to give Android enough time to actually launch App B and have it displayed on screen. If you then launch App C at a later time, it will appear on top of App B.

    2) Launch App B and have App B launch App C itself. You could pass an "extra" in the Intent you use to launch App B and App B can use the presence of this "extra" to know that it should launch App C.