In recent days, the president has engaged in a remarkably public campaign to undercut an active investigation into his presidential campaign, taking to Twitter repeatedly to denounce the inquiry and dismiss it as meritless.

“The Russia-Trump collusion story is a total hoax,” Mr. Trump posted on Monday. “When will this taxpayer funded charade end?”

Comey Tried to Shield the F.B.I. From Politics. Then He Shaped an Election.

As the F.B.I. investigated Hillary Clinton and the Trump campaign, James B. Comey tried to keep the bureau out of politics but plunged it into the center of a bitter election.

But the final straw appeared to be Mr. Comey’s appearance at a Capitol Hill hearing last week, in which he testified that while he was “mildly nauseous” to think he might have influenced the outcome of the presidential election, he had no regrets about his handling of the Clinton emails investigation.

The ill will dates to last summer, when the F.B.I. director took the extraordinary step of going public in the thick of the presidential campaign with his conclusion that Mrs. Clinton could not be prosecuted for her use of a private email server to handle classified documents.

Mr. Trump told aides at the time that Mrs. Clinton should have been charged, according to one person close to him who spoke on the condition of anonymity without authorization to speak for the president. That was a view Mr. Trump often shared with voters on the campaign trail.

“He saved Hillary Clinton from facing justice for her illegal and corrupt actions,” he said of Mr. Comey at a rally in Tampa, Fla. the next month. “They were illegal and they were corrupt, and the F.B.I. saved her, and I would imagine many people within the F.B.I. are extremely embarrassed — extremely.”

The questions did not subside with his victory in November; Mr. Trump ultimately had to be convinced to keep Mr. Comey in place, the person close to him added. Even then, he made it clear he could change his mind at any moment.

In November, Mr. Trump suggested in an interview with CBS’s “60 Minutes” that Mr. Comey’s fate could hinge on whether he was able to satisfy the president that his handling of the Clinton investigation had been justified.

“I would certainly like to talk to him and see him” before making a decision about whether to keep Mr. Comey, Mr. Trump said then. “This is a tough time for him, and I would like to talk to him before I’d answer a question like that.”

The next morning, word spread quickly that Mr. Comey wanted the Justice Department to issue a statement saying that he had no evidence to support the president’s accusation, although no such statement was released.

For weeks afterward, Mr. Trump insisted that his accusation was correct. In dramatic testimony later in March, Mr. Comey said he had no information to back up the president’s allegations even as he confirmed the existence of an F.B.I. investigation into Mr. Trump’s campaign.

That set up a remarkable dynamic — an F.B.I. director directly contradicting a sitting president at the same time that the bureau was pursuing a possible criminal investigation into whether the president’s associates had colluded with Moscow to tilt the election to Mr. Trump.

Last month, the president reiterated that Mr. Comey might not be on solid ground, telling Fox News that “it’s not too late” to fire him, even as he said he had confidence in his F.B.I. director.

“We’ll see what happens; it’s going to be interesting,” Mr. Trump told Fox. “Don’t forget, when Jim Comey came out, he saved Hillary Clinton — people don’t realize that. He saved her life.”

In between, there has been odd body language between the two men. In January during a ceremony at the White House to honor law enforcement officials, Mr. Trump singled out Mr. Comey and called him over, shaking his hand and patting him on the back while whispering something inaudible in his ear.