Zaphod Beeblebrox in 'The Hitchhikers Guide to the Galaxy' used Sunglasses designed to turn completely dark at the first sign of danger, thus preventing him from seeing anything that might alarm him. This does, however, mean that you see absolutely nothing, including where you're going!
For now of course that is fiction but it is possible to make glasses that change from transparent when inside and darken when going outside.
It was Corning that developed Sunglasses and prescription lenses that darken when exposed to the sun in the late 1960s. They became popular under the name Transitions in the 90's. Because the bran Transitions was so popular it lead to them being called transitions generally, instead of the correct term photochromic or photochromatic, as this refers to a type of chemical reaction the lenses have to (UV) or ultraviolet radiation.
A Photochromic lens has millions of molecules of pigments such as silver halide or silver chloride that are embedded in them. These molecules are transparent to visible light where UV light is absent, which is normal for artificial lighting. When exposed to rays of UV, for example direct sunlight, then the molecules undergoes a chemical process that causes them to change appearance. This new molecular structure absorbs parts of the visible light, which causes the lenses to darken. The intensity of the UV rays has an effect on the number of molecules that change shape.
When you go inside and out of the UV light, a different chemical process occurs. The absence of the UV radiation causes the molecules to literally snap back to their original shape, and this result in the loss of their light absorbing properties. The entire process back and forth happens very quickly.
Corning made their original PhotoBrown and PhotoGrey products of glass, and the molecules are distributed evenly throughout the entire lens. This method caused problems in prescription glasses where different parts of the lens were of varying thickness. This meant that the thicker parts of the lens would appear darker than the thinner areas. With the popularity of plastic lenses, a new method was developed. By immersing the lenses in a chemical tank, the photochromatic molecules are absorbed to a depth of about 150 microns into the plastic. This makes it much better than a simple coating, which normally would only be about 5 microns thick so would not provide enough molecules to make the lenses as dark as required. Transitions use this process on the plastic lenses and are the leading manufacturer of photochromic lenses.
It is important to note: because photochromic lenses react to UV light and not to visible light, there are circumstances under which the darkening of the lens will not occur. An example of this is in a car. Because the glass windshield blocks out most UV light, photochromic lenses will not darken. That is why most sunglasses with photochromic lenses also have a certain amount of tint already applied to them.