President Barack Obama had occupied the Oval Office for nearly a full term when government engineers came to him with a request: that he move out for a few months while they completed an overdue renovation of the West Wing.

Valerie Jarrett, Obama’s oldest family friend and a senior adviser, objected.

“Nobody else had to do that,” she said. Other White House staffers were concerned that displacing Obama, though he had no problem with it, could be read as a sign of disrespect for the nation’s first black president.

The moment was a small glimpse into how Obama and his image-makers navigated the sensitive question of race during their eight years in the White House.

On policy and in speeches, Obama showed a determination to pursue equality for all minorities. But he faced resistance — not only from the right but also from allies on the left who felt he wasn’t moving decisively enough. That resistance, along with high-profile instances of racial violence that rekindled a national debate over racial progress, resulted in an uneven record for the Obama administration on the issue of race.

The president leaves office in an age marked by increasing racial tensions. The election to choose his successor featured a backlash reminiscent of the political upheaval of a half-century ago, when the so-called Silent Majority ushered Richard Nixon into office after the turmoil of the 1960s. On Jan. 20, President-elect Donald Trump will take the oath of office in part because a significant number of voters rejected Obama’s quest for broader equality.

Though the election of Obama was celebrated as a moment transcending the country’s dark racial history, the president himself has always taken a more measured view: that race is a complex issue and progress comes in fits and starts.

“After my election, there was talk of a post-racial America,” Obama said in his farewell address last week. “And such a vision, however well-intended, was never realistic. Race remains a potent and often divisive force in our society. … And all of us have more work to do.”

From the beginning of his presidency, Obama made clear to his staff that he wanted to do two things at once — lift up the disenfranchised while moving beyond stereotypes and prejudices. He encountered those personally from the start.

In one of his first White House meetings with Republican lawmakers, one representative lectured the new president — and former senator — on how the legislative process works.

So dismissive was his tone that Obama would marvel afterward to a couple of aides, “He did everything but call me ‘boy.’”

As the meeting broke up, Vice President Joe Biden started to make a beeline for the offender when he felt Obama’s hand on his arm.

“Let’s let that go,” Obama said, stopping him.

Biden attributed Obama’s cool to a lifetime of navigating such dismissiveness.

“His self-awareness is so damn amazing,” Biden said in an interview last month. “It doesn’t mean, I don’t think, he doesn’t remember (or) it doesn’t hurt.” But, Biden added, it suggested that, as a black man growing up in America, Obama had learned when to fight and when to let go.

Another early experience reinforced that. During his first summer in office, police arrested a black Harvard professor and leading scholar of African-American literature, Henry Louis Gates Jr., at his home. Asked about the case, Obama said officers had “acted stupidly in arresting somebody when there was already proof that they were in their own home.”

The backlash from police around the country stunned the president. Aides say the reaction taught Obama he had to speak more carefully on race.

“When the president says it,” one senior aide said, “it has enormous weight.”

Obama instead set about shaping his policy agenda, which over the next eight years would include lasting gains for minorities.

Obama’s signature health care law, the Affordable Care Act, nearly erased the disparity in health care access between black children and white children and helped more than 20 million Americans get health coverage. He signed the Lilly Ledbetter Fair Pay Act, which banned sex-based wage discrimination. His immigration policy has at least temporarily freed millions of Latinos from life in the shadows.

By the administration’s estimate, the economic stimulus law that was one of Obama’s earliest accomplishments kept 5.3 million Americans, a group that was heavily black and Latino, from falling into poverty.

And though he was slower to publicly embrace same-sex marriage than some others, his support for those he warmly referred to as “LGBT folks” helped foster a movement that ultimately saw gay marriage legalized through a Supreme Court ruling. He celebrated the moment in vivid fashion by ordering the White House exterior illuminated in the rainbow colors of the gay pride flag.

Obama pushed for fair-sentencing laws that narrowed the gap in penalties between felonies involving crack and powder cocaine, a divide that had resulted in disproportionately long sentences for blacks. He commuted the sentences of nearly 1,200 people, more than the past 11 presidents combined. The vast majority were drug offenders.

High school dropout rates also fell to historic lows, with African-Americans and Latinos charting the greatest progress.

Other gains are harder to quantify. When the killings of black men by white police officers thrust the issue of race and policing into the national spotlight, he directed the Department of Justice to examine the practices of police at the center of those conflicts, including in Cleveland, Baltimore and suburban Ferguson, Mo. Investigators there found a pattern of abuse against blacks and recommended dozens of reforms.

Additionally, he ordered that surplus military equipment no longer be sent to local governments. The sight of law enforcement officers riding in tanks or conducting raids in riot gear helped contribute to the perception among some that police were invading, rather than protecting, communities.

In early 2012, amid Obama’s campaign for re-election, a shooting in Florida overwhelmed his reticence to speak up amid heated debates on race. Trayvon Martin, an unarmed black 17-year-old, was shot to death by a neighborhood watch captain who told a 911 dispatcher he was suspicious of Martin’s movements.

The shooting seized the nation’s attention, provoking an outpouring of complex emotions.

At the White House, Obama told aides he had something he wanted to say if asked about the shooting, as he was certain to be.

“If I’d had a son,” Obama said, “he’d look like Trayvon.”

Are you sure you want to make it that personal? one adviser asked.

Obama’s answer was to repeat the line, as if considering the sound of it. Yes, he said. He did want to say that.

Uttered later on live television, the remark proved a defining moment in Obama’s tenure; the president had injected himself into a racially charged story for the first time since the Gates episode.

Obama’s description that day of how he had personally grappled with racial injustice was the first of several instances in his second term in which he used his rhetorical gifts to guide the nation through its most emotionally complicated moments.

He broke out into a chorus of “Amazing Grace” in Charleston, S.C., in 2015, during a memorial service for nine people gunned down by a white man at a historically black church. A year later, in Dallas to mourn the shooting deaths of five officers during a peaceful protest of killings of black men in confrontations with police in Louisiana and Minnesota, Obama exhorted both law enforcement and protesters to search for common ground.

“With an open heart, we can learn to stand in each other’s shoes and look at the world through each other’s eyes, so that maybe the police officer sees his own son in that teenager with a hoodie who’s kind of goofing off but not dangerous. Maybe the teenager will see in the police officer the same words and values and authority of his parents.”

By then, the campaign to succeed Obama was shifting into a bitter general election, pulsing with an undercurrent of racial animosity that would culminate in the election of Trump.

The anti-Obama backlash was evident months earlier at rallies for Trump, the most prominent promoter of the lie that the president was born outside the U.S., itself an attempt to delegitimize him.

Trump railed against Obama, complaining that he had presided over a period of “political correctness” that prevented whites from speaking frankly about their concerns and confronting conflict. Trump’s crowds roared their approval.

White nationalists supported Trump more actively than they had any presidential candidate in decades. Trump named as his campaign chairman the chief executive of a website that has given a platform to the so-called alt-right, a collection of openly racist and anti-Semitic activists.

Trump, who kicked off his campaign by saying Mexican immigrants were “criminals” and “rapists,” threatened to ban Muslims from entering the country and said little about his backing from white supremacists, downplaying accusations that he stoked racial unrest.

In Obama’s final year in office, a friend from his days in the Illinois Statehouse told him that he was convinced many of the roadblocks the president had faced were based on racism.

Republicans in Congress “were afraid of you, more so than anything else,” said Denny Jacobs, a white Democrat and longtime state senator from farm country. “They were afraid of you for a couple of reasons. No. 1, you were black. And I talk to my Republican friends — and I’ve got a lot of them — and that’s all they could ever talk about, was the race card, the race. You know, he’s a nice guy, he’s this, he’s that, but he’s black.”

Obama paused and smiled.

“I have no doubt that there are people who voted against me because of race, or didn’t approve of my agenda because of race,” he told Jacobs. “I also suspect there were a bunch of people who were excited and voted for me, or I got political benefits because of the notion of the first African-American president.”

“Those things,” he said, “cut both ways.”