A person should focus fully on his prayer and have proper presence of mind, and remember that he is conversing with his Lord, as al-Bukhaari (405) and Muslim (551) narrated from Anas ibn Maalik that the Prophet (peace and blessings of Allaah be upon him) saw some sputum in the qiblah (of the mosque) and that upset him so much that it could be seen on his face. He scratched it with his hand then he said: “When one of you stands to pray, he is conversing with his Lord, or his Lord is between him and the qiblah, so you should not spit in the direction of the qiblah, rather to your left or beneath your feet.” Then he took the edge of his garment and spat in it, then rubbed part of it against another part, and he said: “Or do like this.”
Leaving gum in the mouth at the very least is contrary to the etiquette of standing before Allaah, may He be exalted, and it may prevent one from reciting properly, or distract the worshipper. If he chews it or if some of it dissolves, then his prayer is invalidated.
Al-Nawawi (may Allaah have mercy on him) said in al-Majmoo’ (4/23): al-Baghawi and others said: Chewing alone invalidates the prayer, even if nothing reaches the stomach, and if a person chews mastic, his prayer is invalidated. If he does not chew it, rather he puts it in his mouth, then if it is new and dissolves it is like sugar and his prayer is invalidated according to the correct view, but if it is used and it does not dissolve, then it does not invalidate his prayer, just as if he held a pebble or a pear in his mouth, it dos not invalidate his prayer. End quote.
And Allaah knows best.