Unable to run anything parallel using ((AndroidDriver) driver).runAppInBackground(Duration.ofSeconds(80));

am testing two Apps A and B . I need to go A keep it open and switch to APP B do some button clicks(while A is still running on background) and come back again to app A.

By the time I switch to app B using

// finish some tasks on B // driver.startActivity(new Activity(“com.example.APP_A”, “.MainActivity”)); The app A is dead. Whatever I expected to happen didn’t happen because its already got killed(I don’t know why)

So I tried ((AndroidDriver) driver).runAppInBackground(Duration.ofSeconds(80)); The above line before I make switch to app B . This works when I stop there and perform manual action in B an come back to A again.

But in automation since the above runAppInBackground method is synchronous …I can’t execute any actions I wanted to do in app B while A is running in background.

Either I wanted that runAppInBackground to be asynchronous method or some way I need to keep my APP A alive during switching to app B ? Any suggestions please ?

Try with -1 value. That should just send app in bavkground.

Ps dont forget that you can use all the power of ADB commands!

Hey Aleksei, I really appreciate your response.
I initially tried with (-1) but it did not work before I moved to (80). Anyways I retried with (-1) now but still it’s the same.
But I was badly looking for these ‘adb commands’ to take control and implement on my own. Can you please shed some light on that and an example please? I am using like this , but not sure what my command should like exact and also how would pass the adb path in the string array ?

		String[] aCommand = new String[] { "adb", "shell", "am", "start","-d", "com.example.APP_B.MainActivity" };
		try {
			Process process = new ProcessBuilder(aCommand).start();
			process.waitFor(10, TimeUnit.SECONDS);
			System.out.println("Starting app B successfully!", hoping app A would now be running in background);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}```

checked a bit old project. Last time when i need switch between apps i did simple quit driver and open driver with new app.

good to know , but when you wanted to come back to the old app again ,does the old app was not still not killed and retain’d its session data?
fyi ., I am able to switch to the different app , but the problem is my old app gets killed and not active.

Update from my side:
Before implementing in Java , I tried running the adb commands manually .

  1. Opened the emulator , and opened the App A.
  2. Went to the terminal , and got into adb path (/Users/XXXXXX/Library/Android/sdk/platform-tools)
  3. run adb shell am start -n com.example.App_B/com.example.App_B.MainActivity
  4. again go back or switch to app A(unresponsive)
    Actual:
    It switched to the new app B. But looks my old app A killed.
    Expected :slight_smile:
    My old app needs to retain its session and should be running in background.

So its still the same behavior as ((AndroidDriver) driver).runAppInBackground(Duration.ofSeconds(-1));

As much as i wanted to know the reason on why these not working I am also looking to replicate /simulate the synchronous method runAppInBackGround() through abd command.
I tried adb shell set com.staples.APP_A RUN_IN_BACKGROUND but didnt work
Kindly help with adb commands for this ?

You may want to look at Reset Strategies for keeping app data intact:

https://appium.readthedocs.io/en/stable/en/writing-running-appium/other/reset-strategies/

plus to @wreed post also look at -> https://github.com/appium/appium-uiautomator2-driver

-> appium:dontStopAppOnReset
-> appium:forceAppLaunch

hey Aleksei,
The adb command is working when from terminal.
But the same command is not doing its job when run programatically from Java and throws no error too.

Is this a way adb command is usually run :
String[] aCommand = new String[] { adbPath, "-s emulator-5554 shell am broadcast -a com.mobile.decode_action --es com.mobile.intentextra.barcode "LOPE" " };

	try {
		Process process = new ProcessBuilder(aCommand).start();
                    process.waitFor(10, TimeUnit.SECONDS);
		Process process2 = Runtime.getRuntime().exec(aCommand);
		process2.waitFor(10, TimeUnit.SECONDS);
		System.out.println("Broadcast event is done.");
	} catch (Exception e) {
		e.printStackTrace();
	}```

to see logs of your command in your console/terminal update with

            Process pb = new ProcessBuilder(aCommand)
            pb.redirectErrorStream(true);
            pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
            pb.start();

Sad, I keep getting unknown adb command. Tried,
String[] aCommand = new String[] { adbPath, "adb shell am broadcast -a com.mobile.decode_action --es com.mobile.intentextra.barcode "09898007" " };

String[] aCommand = new String[] { adbPath, "am broadcast -a com.mobile.decode_action --es com.mobile.intentextra.barcode "09898007" " };

String[] aCommand = new String[] { adbPath, "adb emulator-5554 shell am broadcast -a com.mobile.decode_action --es com.mobile.intentextra.barcode "09898007" " };

String[] aCommand = new String[] { adbPath, "adb", "emulator-5554", "shell", "am", "broadcast", "-a", "com.mobile.decode_action", "--es", "com.mobile.intentextra.barcode", "\"09898007\"" };

while this one works perfect in terminal: "adb shell am broadcast -a com.mobile.decode_action --es com.mobile.intentextra.barcode "09898007" "

adbPath = Library/Android/sdk/platform-tools/adb

you execute code on remote machine or ? look here how to add path! → bash - java process builder add path to environment not working - Stack Overflow

and this does not look correct! it is not FULL path.

Yes the full path would look like Users/xxxx/Library/Android/sdk/platform-tools/adb ? I have it on my code.
By the way , i am running locally on the same machine

Thanks Aleksei &Wreed both of you and One of my colleague Philip,Han who helped me set the correct PATH. FYI , if anyone looking for solution here set the path right by figuring out the correct RC and pass them to Processbuilder along with the PATH params in environment and the issue is resolved.

1 Like