There is an obligation to stand when one’s father or mother enters the room where a person is, and to remain standing until the parent reaches his or her place or sits down. It occasionally happens that a person in any event was planning to leave the room at the time when his parent entered the room. In such a case, when the person stands, it is not evident that he stands to give honor to the parent, as it may appear that he stood simply because he had to leave the room.
Therefore, the Ben Ish Hai (Rav Yosef Haim of Baghdad, 1833-1909), based upon a ruling of the Sefer Hasidim (Rabbenu Yehuda Ha’hasid, Germany, d. 1217), writes that it is proper in this case for the person to first sit before leaving the room. Meaning, if one’s parent enters the room just as he was about to stand up and leave, he should stand to honor his parent and then sit momentarily when the parent reaches his or her place. He may then stand up and go where he intended to go. This way, it is clear that he stood to give the parent honor, and not for some pragmatic purpose.
The Ben Ish Hai makes an exception in the case of somebody who needs to use the restroom. There is a Halacha of "Bal Teshaketzu" which forbids restraining oneself when he needs to perform his bodily functions. In such a case, then, it is preferable not to first sit down, but to rather immediately leave the room to care for his bodily needs. In most cases, however, as mentioned, one should sit down for a moment after the parent reaches his or her place before leaving the room.
Summary: If one’s parent enters the room just as he was in any event about to stand up to leave, he should sit momentarily after rising to honor his parent, and only then leave, so it is clear that he stood for his parent’s honor and not because he needed to leave. However, if he needs to use the restroom, then he should leave immediately, rather than first sit momentarily.