I had this happen with a fresh Denso rebuild from Rock Auto. It tested good and would function properly on the bench. Solenoid threw the shaft outwards and all. Once I installed it in the car, it would just spin freely without engaging the flywheel. Very frustrating.
If it's a Toyota starter, I'd try to rebuild if you can. If it's already non-OEM I'd just buy another reman - perhaps not from Rock Auto though. Mine was a bear to return and get credit on. I'm still bitter about that, years later.