Yes, you can be offside by coming back into an onside position, but the actual offence occurs at your position when the ball is played , not where you receive it, and that's where the free kick is taken from. Otherwise offside players would just wait to receive the ball until they reach the corner flag to force a free kick deep into the opponents' half.
Don't try to tell me it makes sense to take an offside free kick from a position where it's impossible to be offside. So you reckon if a player comes from an offside position on the halfway line and receives the ball on the edge of his own box, then the other team gets an indirect free kick at the edge of the penalty box?