Push app in background and then bring the app back into foreground

I need to implement the following scenario in Python for both Android and iOS:
A. I have a native app already opened on a real device.
B. I want to push the app in the background in order to have access to Home screen of the device.
C. I want to switch wifi to ON or OFF (I implemented already this step)
D. I want to add the app back in the foreground exactly from where I remained (without reset, reinstall etc).

I’m struggling with steps B and D.

Lets take this example:

  1. self.driver.background_app(15)
  2. print(’ Now lets switch Wi - Fi’)

If I’m using driver.background_app(), I noticed that print() is executed only AFTER the 15 seconds passed and the native app is already again in the foreground. But I want to reach step 2 WHILE the app is still in the background, to execute print (or in my real case to switch WiFi) and only after that to bring the app back in the foreground.

Also is there a solution to push the app in the background without giving the number of seconds and EXACTLY after I finish the steps between (like switching WiFi), to add the the app back in the foreground with maybe another command?

I hope it makes sense the above scenario…

you can set background duration to -1, so then the app won’t be restored. Then use activateApp method in order to put the app back to foreground

Thank you for your answer, I tried this already, but until now I’m getting errors when trying to run: self.driver.activate_app (I think this is the equivalent in Python): “selenium.common.exceptions.WebDriverException: Message: unknown command (Unknown command)”

make sure both client and server are up to date