The less the ability of the molecules of a substance to pass from a non-ionized to an ionized state (from non-polar to polar), that is, dissociation, the worse the absorption of the drug and the lower the bioavailability index. The factor regulating the dissociation processes is the pH value in various parts of the gastrointestinal tract. So in different phases of gastric secretion, the pH can vary within normal limits, so some drugs are drunk before, during and after meals.
Also, some medicinal substances (if they are not covered with an enteric coating) can interact with food or have irritating effect on the gastric mucosa.
As far as I understand, on an empty stomach, absorption is better. And during or after meals - mainly when the drug irritates the stomach lining (for example, non-steroidal anti-inflammatory drugs like aspirin, or most iron preparations).