This being said, the activity of the magnetic field is also affected by the orbit of the Sun. When the Sun intersects the arm of our galaxy, the magnetic field is disturbed by the (relatively) close encounters with stars and other objects. This causes the magnetic field to be weak more than usual. During a gap between arms, there are little or no magnetic flips. Therefore, the field can be strong for millions of years in a row. This theory is reflected in Earth's history. For example, in the Cretaceous period, the Sun went through the longest empty gap in the galaxy. The magnetic field was quiet for nearly 40 million years. Then the magnetic field flipped five times in the next 20 million years. During this time, a 180 km meteor wiped out of 80% of life on Earth, including the dinosaurs. Also, a similar event occurred in the Permian period. After a 60 million year quiet zone, ten flips happened in the next ten million years. As the rapid flipping was occurring, a space rock more than 300 km across, one of the largest in history, destroyed over 95% of life on the planet. Note this this theory is not widely accepted at the current time.

Map of the Milky Way. The two events mentioned above are caused by the gap after the Outer Arm going clockwise.
If this theory is incorrect, another possible explanation is the dynamo theory. This theory is based on the fact that nearly everything (except some subatomic particles) have magnetic fields. This is due to the presence of electrons (up to the present, not much is known about where electrons get their charge, as this is one of the fundamentals of the very early Universe). Since the Earth has an iron core, a magnetic field is formed around that. But this is still not enough to explain the magnetic field around Earth, and this is here the dynamo theory comes in.
A combination of the convection on molten iron in the outer core and the Coriolis effect (the same force that allows low pressure systems to form, causing weather) creates powerful electric currents. Then, since electricity and magnetism are connected (see electromagnetism), another magnetic field is produced on top of the first one. With one field sustaining the other, a dynamo is formed. Other examples of dynamo in celestial bodies are conducting plasma in the Sun and other stars, and in active galactic nuclei (based on supermassive black holes). Disturbances in this magnetic field causes it to flip or reverse when the molten outer core's circulation is disrupted (e.g. a major asteroid impact, which gives a valid contradiction to the theory described earlier). The dynamo theory also explains the flipping of the solar magnetic field, which happens every 7 to 15 years, which is very often in contrast to the Earth's. The dynamo theory is the most widely believed at this point in time.