The dhikr for entering the bathroom should be said before entering it, not after
Question

Should we say the supplication for entering the bathroom before entering or after entering?
Praise be to Allah.The Sunnah for the one who wants to enter the bathroom is to recite the recommended dhikr before entering it and not after entering. This is indicated by a number of hadiths, including the following:
It was narrated from Anas ibn Malik (may Allah be pleased with him) that when the Prophet (blessings and peace of Allah be upon him) wanted to enter the outhouse, he would say: “Allahumma inni a`udhu bika min al-khubthi wa’l-khaba’ith (O Allah, I seek refuge with You from the male and female devils).” Narrated by al-Bukhari, 142.
It was narrated that Zayd ibn Arqam (may Allah be pleased with him) said: The Messenger of Allah (blessings and peace of Allah be upon him) said: “These toilets are haunted (frequently by devils), so when one of you wants to enter [the toilet] let him say, ‘A`udhu Billahi min al-khubthi wa’l-khaba’ith (I seek refuge with Allah from the male and female devils).” Narrated by Abu Dawud, 6; classed as authentic by al-Albani in Sahih Abi Dawud.
The word “wants to” clearly indicates that it is prescribed to recite the dhikr before entering and not after.
This is also indicated by the fact that it is disliked (makruh) to recite the dhikr in the place where one relieves oneself. This has been explained previously in the answer to question no. 23308.
The jurists (may Allah have mercy on them) have stated that clearly.
Al-Khatib ash-Sharbini (may Allah have mercy on him) said:
It is recommended to say [it] when wanting to enter the outhouse, or when reaching a place where one wants to relieve oneself, such as a desert, and so on.
End quote from Muhni al-Muhtaj, 1/159-160.
In says in Minah al-Jalil, 1/99, which is one of the books of the Malikis:
It is recommended to recite the dhikr before entering the place where one relieves oneself, which is: “Bismillah. Allahumma inni a`udhu bika min al-khubthi wa’l-khaba’ith ar-rijsi an-najisi ash-shaytan ar-rajim (In the name of Allah. O Allah, I seek refuge with You from the male and female devils, from the evil and filthy accursed Shaytan). End quote.
It says in al-Mawsu`ah al-Fiqhiyyah, 8/88:
The jurists are unanimously agreed that it is prescribed in the sense of being recommended to say Bismillah before entering the bathroom to relieve oneself. End quote.
In Fatawa al-Lajnah ad-Da’imah, 5/93, it says:
Part of the etiquette of Islam requires a person to remember his Lord when he wants to enter the toilet or bathroom, by saying before he enters it: Allahumma inni a`udhu bika min al-khubthi wa’l-khaba’ith (O Allah, I seek refuge with You from the male and female devils), and he should not recite the dhikr after entering it; rather he should stop reciting it as soon as he enters it. End quote.
And Allah knows best.