It was a message to young African-Americans, the ones who wanted to protest the injustices they saw around them. The president had invited some of them to the White House to talk with him about their cause, and in a few cases, they refused the invitation. People close to him said that made him burn.

“You can be completely right, and you still are going to have to engage folks who disagree with you,” Mr. Obama told the students that day, speaking more to members of the Black Lives Matter movement than to the graduates before him. Being “as uncompromising as possible, you will feel good about yourself, you will enjoy a certain moral purity, but you’re not going to get what you want.”

For the president, a onetime community organizer in Chicago, it was a crucial distinction. Mr. Obama had tilted against the powerful and connected — he called his time as an organizer “the best education I ever had, better than anything I got at Harvard Law School.” His approach had always been more strategic than confrontational and foreshadowed his cautious, meticulous attitude in the White House.

In the speech, Mr. Obama recalled working as a young Illinois state senator to fight racial profiling, not by calling the police racist, but by sitting down and negotiating a new law. “So we engaged and we listened, and we kept working until we built consensus,” he said.

By the time he became president, people close to Mr. Obama said, he was frustrated by the idea that young activists would view themselves as tainted if they came into the White House to talk to him. In the speech, he urged the young protesters not to give in to the idea that the system is rigged.

“That will lead to more cynicism, and less participation, and a downward spiral of more injustice and more anger and more despair,” he said. “And that’s never been the source of our progress. That’s how we cheat ourselves of progress.”

“Listen,” he told the students. “Engage. If the other side has a point, learn from them. If they’re wrong, rebut them. Teach them.”