Run app in background command executed twice when putting the app in background for more than 120 seconds

@mykola-mokhnach

I could see the runAppInBackground command executed twice when putting the app in background for 120 seconds. (Tried 119 seconds as well. It’s behaving in the same way). But When I put the app in background for 115 seconds, it’s executed only once - works as expected.
You could see the adb command adb shell monkey in the logs while appium started executing the runAppInBackground command second time. May I know what is the issue here?

I’ve tried in android device and using latest appium version: 1.11.0. In older versions as well I’ve faced the same issue when increasing the background time.

Put app in background for 120 seconds

driver.runAppInBackground(Duration.ofSeconds(120))

[HTTP] → POST /wd/hub/session/b8a82cab-0535-434b-b9c6-ea7cb97ee582/appium/app/background
[HTTP] {“seconds”:120}
[debug] [W3C (b8a82cab)] Calling AppiumDriver.background() with args: [120,“b8a82cab-0535-434b-b9c6-ea7cb97ee582”]
[debug] [ADB] Getting focused package and activity
[debug] [ADB] Running ‘/home/anto/Android/Sdk/platform-tools/adb -P 5037 -s ce12171cd031b8050c shell dumpsys window windows’
[debug] [ADB] Pressing the HOME button
[debug] [ADB] Running ‘/home/anto/Android/Sdk/platform-tools/adb -P 5037 -s ce12171cd031b8050c shell input keyevent 3’

TRYING SECOND TIME:
[debug] [AndroidDriver] Activating app ‘x.x.x’ in order to restore it
[debug] [AndroidDriver] Activating ‘x.x.x’ with ‘adb shell monkey -p x.x.x -c android.intent.category.LAUNCHER 1’ command
[debug] [ADB] Running ‘/home/anto/Android/Sdk/platform-tools/adb -P 5037 -s ce12171cd031b8050c shell monkey -p x.x.x -c android.intent.category.LAUNCHER 1’
[HTTP] ← POST /wd/hub/session/b8a82cab-0535-434b-b9c6-ea7cb97ee582/appium/app/background - - ms - -
[HTTP]
[HTTP] → POST /wd/hub/session/b8a82cab-0535-434b-b9c6-ea7cb97ee582/appium/app/background
[HTTP] {“seconds”:120}
[debug] [W3C (b8a82cab)] Calling AppiumDriver.background() with args: [120,“b8a82cab-0535-434b-b9c6-ea7cb97ee582”]
[debug] [AndroidDriver] Command stdout: Events injected: 1
[debug] [AndroidDriver] ## Network stats: elapsed time=28ms (0ms mobile, 0ms wifi, 28ms not connected)
[debug] [ADB] Getting focused package and activity
[debug] [ADB] Running ‘/home/anto/Android/Sdk/platform-tools/adb -P 5037 -s ce12171cd031b8050c shell dumpsys window windows’
[debug] [W3C (b8a82cab)] Responding to client with driver.background() result: true
[debug] [ADB] Pressing the HOME button
[debug] [ADB] Running ‘/home/anto/Android/Sdk/platform-tools/adb -P 5037 -s ce12171cd031b8050c shell input keyevent 3’
[debug] [AndroidDriver] Activating app ‘x.x.x’ in order to restore it
[debug] [AndroidDriver] Activating ‘x.x.x’ with ‘adb shell monkey -p x.x.x -c android.intent.category.LAUNCHER 1’ command
[debug] [ADB] Running ‘/home/anto/Android/Sdk/platform-tools/adb -P 5037 -s ce12171cd031b8050c shell monkey -p x.x.x -c android.intent.category.LAUNCHER 1’
[debug] [AndroidDriver] Command stdout: Events injected: 1
[debug] [AndroidDriver] ## Network stats: elapsed time=23ms (0ms mobile, 0ms wifi, 23ms not connected)
[debug] [W3C (b8a82cab)] Responding to client with driver.background() result: true
[HTTP] ← POST /wd/hub/session/b8a82cab-0535-434b-b9c6-ea7cb97ee582/appium/app/background 200 121545 ms - 14

1 Like

@mykola-mokhnach Any updates on this?

@Anto_Rodrigo hi. Can you pls put light into it? E
Why you need put app into background? Can you e.g. put into backgroung with value 0 = forever. And after needed time just restore app back?

Hi @Alessko Thanks for your reply. I’m putting the app in background for collecting some data as per our testing app’s feature.
I’ve tried like driver.runAppInBackground(0) as you said, with 0. But app goes background and suddenly back to foreground. Here 0 means 0 seconds and I believe that it will not put the app in background forever. Actually, I just wanted to know why is the runAppInBackground function behaving in this way.

Sorry for confusion. I was thinking about “-1” but wrote “0”.

@Alessko Thanks for letting me know about -1. That works like a charm. I just adjusted my code according to this.

driver.runAppInBackground(Duration.ofSeconds(-1));
sleep(130 * ONE_SECOND); // 130 * 1000 - 130 seconds
driver.activateApp(“x.x.x”); // x.x.x - bundle id

And @mykola-mokhnach Thanks for referring the same code regarding this. Might be -1 is an alternate solution for this. But I didn’t get the exact answer for the created issue - run app in background happening twice when reaching 120 seconds. Anyway thanks for the alternate solution and if you find the exact reason kindly post here, so that I can tackle if I need anything regarding this in future.

As you can see there the logic is quite complicated. The behaviour also depends on the fact that some additional args were passed to the activity. Plus it snapshots the activity, that was the current one before putting the app to the background, and which might be different from the default one.

Anyway, you are welcome to debug the stuff and propose a fix. Right now I don’t observe any anomalies looking at that piece of code, but you might find a better solution by debugging it.

Okay thanks for the info @mykola-mokhnach. I’ll try to debug and post here if I find any better solution.