Flags are placed or changed when there is only a single colour of military unit in a taxable area. There are no other situations when they are placed or changed.
Are you sure? Let's say that there are two red fleets with their flag and one friendly blue fleet in one area. Four green fleets come and destroy the red fleets, blue and green want to be friendly. Does red keeps its flag even if it is not present?