Although many material designs or strategic methods have been proposed for treating oil spills and oily wastewater, the complex oily state, dealing with the harsh operating conditions of oil–water separation (such as the recovery of viscous spilled crude oil, bacteria-containing oily wastewater, and removal of spilled oil under fire), and the autorecycling of oil and absorption materials remain a great challenge. This paper proposes an ingenious design strategy of “several birds with one stone” to prepare pH/thermoresponsive flame-retardant/photothermal bactericidal P-Fe3O4-polydopamine (PDA)@ melamine–formaldehyde (MF) foams. This design makes the foams remarkably effective in the recovery of spilled viscous crude oil as well as in the separation of bacteria-containing oily emulsions, particularly for instant fire extinguishing by magnetically controlled oil absorption as well as for fire alarms. The photothermal effect and pH response induce a change in the surface wettability of the foams, facilitating excellent autoadsorption/desorption of the spilled oil. The photothermal bactericidal activity and fouling resistance of the foam are beneficial to the separation of bacteria-containing oily wastewater. Outstanding flame-retardant properties and maneuverable magnetic control enable the foam to rapidly recover the spilled oil in a large range of fires, extinguish fires instantly, and facilitate early fire warning. The proposed strategy is expected to inspire further research on treating oil spills under complex conditions.