Q:

MonoGame SetRenderTarget is wiping the backbuffer

In my Monogame (XNA) Windowed App, compiled for Windows PC, running on Windows 7, I have
public RenderTarget2D renderTarget = null;

In the Inititate() function I have
renderTarget = new RenderTarget2D(graphicsDevice, 200, 200, false,
                           graphicsDevice.PresentationParameters.BackBufferFormat,
                           graphicsDevice.PresentationParameters.DepthStencilFormat);

My drawing is as follows
public override void Draw(GameTime gameTime)
{
    base.Draw(gameTime);

    // POSITION A

    // draw some stuff to the back buffer
    graphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer,
                         backgroundColor, 1, 0);

    SceneRoot.Draw(gameTime);    // draw some 3d primitives

    spriteBatch.Begin();
    spriteBatch.Draw(...some things...);
    spriteBatch.End();

    // POSITION B

    spriteBatch.Begin();
    spriteBatch.Draw((Texture2D)renderTarget, new Vector2(5f, 200f),
                     new Rectangle(0, 0, renderTarget.Width, renderTarget.Height),
                     Color.White);
    spriteBatch.End();
}

If I put the following code to draw to the renderTarget in POSITION A then I can see the stuff drawn between POSITION A and POSITION B, with the stuff drawn to the renderTarget on top, as expected.
If I put the following code in POSITION B then I can't see anything drawn between POSITION A and POSITION B, just the stuff drawn to the renderTarget.
graphicsDevice.SetRenderTarget(renderTarget);
graphicsDevice.Clear(Color.Red);
spriteBatch.Begin();
spriteBatch.Draw(...some things...);
spriteBatch.End();
graphicsDevice.SetRenderTarget(null);

With the above code for insertion, I can even reduce it to 
graphicsDevice.SetRenderTarget(renderTarget);
graphicsDevice.SetRenderTarget(null);

and still get the same result (only see what is drawn to the renderTarget, which is now nothing, ie a blank screen). I've tested, and it's the SetRenderTarget(renderTarget) that's causing the problem, seemingly by wiping the backbuffer.
Oh, and none of the sprites drawn cover the whole display, so it's not the sprites displaying wrong, I don't think, as I should still see the 3d stuff.
I've looked and I don't see anything in SetRenderTarget about wiping the backbuffer. Can anyone help? Is it something else, like something to do with the RenderState or the Effects? I can provide more of my code if need be.

A:

I am not 100% sure about the Monogame implementation. But, in XNA, what you are seeing is the correct behaviour. One would expect Monogame to be faithful to the XNA implementation.
The reason this behaviour exists is that on some devices (Xbox 360, WinPhone) the operations for saving and restoring a backbuffer are slow. So the default is to clear a render target (including the backbuffer via null) whenever it is set with SetRenderTarget. Even on hardware that does not require a clear, it is cleared anyway so that your code works cross-platform.
The behaviour is controlled by passing RenderTargetUsage (MSDN) to the RenderTarget2D constructor (MSDN) or setting it in your PreparingDeviceSettings handler (MSDN; the thing to set is args.GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage).
But, rather than modifying the RenderTargetUsage, it is generally better to simply draw to your render targets in an order that respects dependencies.
(And, yes, the XNA documentation is terrible for not mentioning this anywhere.)

