Strictly, the window that was active immediately before yours was the shell. Your application was started by the shell, and what you are really asking is what was the application that was active before the shell. Now that's an ask!
It's easy to enumerate the top level windows, which is basically what the shell does to give you the task list. That gives you a list of candidates. Not so easy to know which one, although the order in which they are returned could be a clue.
It's also relatively easy to get the Z-order for the running applications. It's highly likely that the application that was running before yours is the one with the highest Z-order, excluding your app and the shell.
My guess is that would be close enough for most purposes. After that I'm out of ideas.