TOKYO — It was just after 6 a.m. on the final day of a state visit to Japan designed to flatter and entertain him, and President Trump was in his comfort space: Twitter.
Hours earlier, he had been the honored guest at an extravagant six-course dinner — turbot, steak, a Mount Fuji-shaped dessert — with Japan’s new emperor at the Imperial Palace in Tokyo. Ahead of the president was a visit to a military base near the capital, his fourth straight day at the side of Prime Minister Shinzo Abe.
But Mr. Trump’s mind was, again, preoccupied by politics at home.
“Super Predator was the term associated with the 1994 Crime Bill that Sleepy Joe Biden was so heavily involved in passing,” he wrote of Joseph R. Biden Jr., the Democratic presidential front-runner. “That was a dark period in American History, but has Sleepy Joe apologized? No!” The day before, his mind had been on Democratic “obstructionists,” possible “treason” by Russia investigators, and the perfidy of the “fake news media.”
Mr. Trump traveled almost 7,000 miles to become the first foreign leader to meet with Emperor Naruhito since his enthronement this month. The president’s closest ally on the world stage, Mr. Abe, regaled him with golf, a sumo tournament, a cheeseburger lunch and a robatayaki dinner, hoping to cement what the prime minister described as their “unshakable bond.”
Throughout his visit, though, Mr. Trump acted like a man who could never be fully present. From start to finish, his stay in Japan was defined more by his focus on politics at home than diplomacy abroad, expressed as a running refrain posted online seemingly every time he was left alone with his screens.
From his particular fixation on Mr. Biden to his constant castigation of Democrats over all, Mr. Trump underlined the reality that his “unshakable bond” was with his Twitter megaphone. It was evident that his main interest was not where his hosts had gone to such lengths to direct it — on security and trade in Asia — but instead was on fighting with his perceived political enemies in Washington.
American officials in the past have made a point of leaving domestic politics behind when traveling abroad. Politics, as the outdated saying goes, stopped at the water’s edge. Not so in the age of Trump and Twitter.
“Presidents abroad represent all the people, not just their supporters,” said William Kristol, the conservative columnist and prominent Trump opponent. “And they speak for the United States, not just for their party. Or at least they once did.”
Shortly before he departed for the imperial banquet, Mr. Trump logged on to Twitter from his hotel, saying the Democrats were getting nothing done and “only want a Do-Over on Mueller!”
“Impeach for what, having created perhaps the greatest Economy in our Country’s history, rebuilding our Military, taking care of our Vets (Choice), Judges, Best Jobs Numbers Ever, and much more?” he wrote.
Mr. Trump’s visit to Japan was not the first time he has gone abroad and made life difficult for the leader who was hosting him.
Last year, he arrived for a diplomatic visit to Britain and greeted Prime Minister Theresa May with a scathing interview in which he called her approach to Brexit a betrayal of British voters and said that Boris Johnson, a critic of Mrs. May, would make a “great prime minister.”
Mrs. May smiled gamely through the visit, during which Mr. Trump tried to walk back his comments, deeming the interview “fake news.”
Mr. Abe employed a similar approach in recent days, as Mr. Trump offered the latest split screen between carefully coordinated diplomacy on the ground and an airing of grievances in the cloud.
After presenting a trophy at a sumo tournament — an eagle-topped object he said he hoped the Japanese would treasure for centuries — Mr. Trump returned to Twitter and appeared to quote content from the Fox News show hosted by Jeanine Pirro. “Why doesn’t the press apologize to President Trump for the Russian Collusion Delusion?” he wrote. “How about the Dems also?”
The worst insult to his hosts came in a tweet that ultimately defined the trip. In it, Mr. Trump played down concerns about North Korea’s recent tests of short-range ballistic missiles, undercutting declarations by Mr. Abe and the president’s own national security adviser that the launches violated United Nations resolutions.
“I have confidence that Chairman Kim will keep his promise to me, & also smiled when he called Swampman Joe Biden a low IQ individual, & worse,” Mr. Trump wrote early on the first full day of his visit, referring to Kim Jong-un of North Korea, before a round of golf with Mr. Abe. “Perhaps that’s sending me a signal?”
In a joint news conference with Mr. Abe the next day, Mr. Trump broke the third wall, expressing out loud on the world stage the same sentiments he had been tapping out throughout the trip online.
“Kim Jong-un made a statement that Joe Biden is a low-I.Q. individual,” Mr. Trump said from the ornate Akasaka State Guest House. “He probably is, based on his record. I think I agree with him on that.”
After addressing American troops aboard the U.S.S. Wasp aircraft carrier in honor of Memorial Day, Mr. Trump flew home. Despite the pageantry, it in some ways felt as if he had never left Washington.
“I can never remember such a moment in modern American diplomacy,” said Michael McFaul, a former American ambassador to Russia. “It’s completely unprecedented, especially in the vulgar ways that President Trump choose to do so.”
“Such behavior,” he added, “does not serve American national interests and puts our hosts — in this case Prime Minister Abe — in a very awkward position.”