Popping out of gear
There are two, and only two, primary reasons for this.
1. The selected gear is not fully engaged. This can be caused by worn or poorly adjusted linkage, body interference, or internal clearances among other things. If you are sure that you are fully engaging the selected gear, then you have
2. Worn backcut on the speed gear and/or facing hub. These backcuts are engineered into these pieces to prevent this very condition, but if they become worn, usually due to an item in reason #1, the only real option is to replace them and correct the root cause. I will try to find some photos for you. All I can say is once you see it, it makes perfect sense.
I have seen great effort involved in detent balls, springs, rails, and other clever attempts to correct for this, and all of them simply prolong the misery.
Sorry if this is all a bit much. If you are not comfortable with the diagnosis, it's best to find a respectable gearbox tech in your area for help. That can be a problem all in itself...
Good luck!