Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

WASHINGTON — Making his first appearance on the campaign trail since leaving the White House, former President Barack Obama on Thursday avoided directly criticizing President Donald Trump while stumping in New Jersey for the state's Democratic nominee for governor.

Obama called on voters to reject the "old politics of division ... that dates back centuries" in the Nov. 7 election.

"Some of the politics we see now, we thought we put that to bed," he told a mostly African-American audience in Newark. "It's the 21st century, not the 19th century. Come on!"

In Richmond, Virginia, on Thursday night, Obama said "folks don't feel good right now about what they see" in government. "They don't feel as if our public life reflects our best."

"Instead of our politics reflecting our values, we've got politics infecting our communities," he said, referring to people who "demonize" others with different ideas to "get the base all riled up because it provides a short-term tactical advantage."

Although his attacks were trained on Ed Gillespie — who's challenging Ralph Northam in Virginia's gubernatorial race — it was clear that they also applied to Trump.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

While he skewered Gillespie for using the "same old playbook" of negative ads and pushing divisive issues into the public discourse, the former president told an enthusiastic crowd: "If you have to win a campaign by dividing people, you're not going to be able to govern," nor will you be able to "unite them later if that's how you start."

Obama has rarely spoken publicly about politics since Trump took office, respecting the longstanding omerta usually observed by the tiny club of former Oval Office occupants.

Obama's appearances on behalf of Phil Murphy in Newark and for Northam in Virginia were designed to energize Democrats and black voters ahead of the two biggest elections of the year.

The affection for Obama was palpable as soon as he hit the stage in Newark to chants of "four more years."

"I refer you both to the Constitution, as well as to Michelle Obama, to explain why that won't happen," he joked Thursday afternoon.

Murphy, a former Goldman Sachs executive and ambassador to Germany under Obama, is heavily favored to succeed term-limited Republican Gov. Chris Christie. But Obama told the audience to ignore the polls.

"You can't take this election or any election for granted — I don't know if you all noticed that," he said with a smirk, referring to the 2016 presidential race and Trump's victory. "You've got to run through the tape."

Phil Murphy, the Democratic candidate for governor of New Jersey, with former President Barack Obama in Newark on Thursday. Spencer Platt / Getty Images

Obama's second campaign stop of the day had higher stakes.

Northam needs strong turnout from African-American voters to succeed in a tight race against Republican Ed Gillespie.

"Barack Obama coming into Virginia tonight is just very exciting, and he's going to energize our voters and just kind of close that loop for us," Northam told MSNBC earlier in the day.

Hillary Clinton and other Democrats have struggled to keep black voters as energized as they were when Obama was on the ticket. Both Murphy and Northam are white.

Virginia has been trending Democratic since Obama won it in 2008, with the party now controlling both of its Senate seats and all of its statewide offices. But recent polls have concerned Democrats that 2017's marquee election may be slipping away.