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.”