Animals explore their environment to encounter suitable food resources. Despite its vital importance, this behavior puts individuals at risk by consuming limited internal energy during locomotion. We have developed a novel assay to investigate how food-search behavior is organized in Drosophila melanogaster larvae dwelling in hydrogels mimicking their natural habitat. We define three main behavioral modes: resting at the gel's surface, digging while feeding near the surface, and apneic dives. In unstimulated conditions, larvae spend most of their time digging. By contrast, deep and long exploratory dives are promoted by olfactory stimulations. Hypoxia and chemical repellents impair diving. We report remarkable differences in the dig-and-dive behavior of D. melanogaster and the fruit-pest D. suzukii. The present paradigm offers an opportunity to study how sensory and physiological cues are integrated to balance the limitations of dwelling in imperfect environmental conditions and the risks associated with searching for potentially more favorable conditions.