“If you think that the only way forward is to be 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,” the president said, adding that such an approach leads to “a downward spiral of more injustice and more anger and more despair, and that’s never been the source of our progress.”

The president’s commencement speech — one of three he will deliver this year — was written to envision a future in which the young Howard graduates will take over from Mr. Obama’s generation. But for a president nearing the end of his time in office, it also served as a reflection on his own successes and failures.

Mr. Obama’s tenure has been filled with compromises: health care legislation that fell short of his ideal, immigration executive actions that are limited in scope because of congressional opposition, economic measures that were tempered by Republican ideology, and foreign policy ambitions held back by allies and adversaries alike.

Without mentioning them specifically, Mr. Obama on Saturday offered a defense of his legacy of incremental progress, saying that “I always tell my staff, better is good, because you consolidate your gains and then you move on to the next fight from a stronger position.”

The president praised Brittany Packnett, a leader in the Black Lives Matter movement, for being willing to participate in discussions with big-city police chiefs and others in search of a solution to the violence that has too often erupted between African-American young people and the police. She did so, he said, despite criticism from other activists who questioned her participation.