If it’s for Android try using adb commands:
Call this method twice and you will be able to switch between apps
public static void switchApp(){
runCommand(“adb shell input keyevent KEYCODE_APP_SWITCH”);
}
here is the runCommand:
public static String runCommand(String command){
String output = null;
try{
Scanner scanner = new Scanner(Runtime.getRuntime().exec(command).getInputStream()).useDelimiter("\A");
if(scanner.hasNext()) output = scanner.next();
}catch (IOException e){
throw new RuntimeException(e.getMessage());
}
return output;
}