those cups are a bit small for raising fruitflies. Like you say they will all escape when you open it. I use sweet jars or tall ice cream tubs to raise mine with a little cup filled with medium in them.
I would recommend that once you get your next cup, try and transfer as many flies to your bigger container with the medium in it.
Flies will still escape but not as many and a slight tap to the container before opening makes the flies fall down.
There are lots of recipes to use as a medium but the one I use these days is as follows.
I add about 3 parts boiled mashed potato, 3 parts oats and 1 part cider vinegar (this prevents any mould and even feeds the flies). Then I add a few slices of orange (about 1 part again, orange is the best fruit to use as it is acidic and naturally inhibits mould too) and add water, mixing it all up to create a sloppy paste but not too sloppy. It should be damp looking but not so wet that it turns into quicksand for flies.
Once that is done I get a very small cup of warm water and add a bit of sugar. On my yeast tin it says 1 tablespoon to 150ml of water. If you use 150ml it says to add 1 tablespoon of yeast. Just follow the instructions of your yeast tin and let the yeast activate itself for 10 minutes, then add a few drops of this yeast mixture onto the medium. The yeast will soon start multiplying on the medium and will feed the flies.
Some people add a bit of sugar to their medium also.
In my opinion it is best to activate the yeast before adding it to the medium.