To answer this first we need to know why there is a cavity in the first place. Houses were built with cavity walls so that a layer of poorly conducting air is trapped between the two walls, in the cavity. This reduces the amount of heat lost directly through the walls. But the cavity is big and the air can move about. Filling the cap with a foam still leaves a gap filled maily with air (or a gas which is a poor conductor) but stops the air moving about so reducing further the lass of heat through the wall.
Hope this answers you question. Hamish