BERLIN — Angela Merkel had meetings with not one but two American presidents in her calendar on Thursday — and both could help her win re-election, but for different reasons.

Before heading to Brussels for a meeting with fellow NATO leaders including U.S. President Donald Trump, the German chancellor sat down in Berlin for a public discussion with his predecessor, Barack Obama, against the backdrop of the Brandenburg Gate.

Obama didn't take long to offer a strong endorsement of Merkel's leadership — just the sort of signal her Christian Democrats had been hoping for. Unlike her visitor from the United States, Merkel still has another election to win and Obama remains highly popular in Germany.

“Not only do I love this city, but one of my favorite partners throughout my presidency is sitting next to me today,” Obama said. “Chancellor Merkel has done outstanding work, not just here in Germany, but around the world.”

In front of 70,000 people gathered for a church convention, Merkel and Obama spent 90 minutes on stage discussing a broad range of topics including U.S. health care reform, military drones and migration.

Asked about refugees, Merkel rejected criticism over her increasingly tough stance on the issue.

"I know that this doesn't win me popularity here,” Merkel, a pastor's daughter who grew up in communist East Germany, said when asked about deportations to crisis-ridden countries such as Afghanistan. "But ... we need to make sure that we [only] help those who need our help, of whom there are many in the world."

Obama backed Merkel's stance, adding that throughout the eight years of his presidency, he had constantly felt the dilemma of reconciling feelings of compassion with other responsibilities he held as U.S. president.

One of the key solutions, Obama said, was to improve living conditions for people in their countries of origin so they don't feel the need to leave their homes.

"We can't isolate ourselves. We can't hide behind a wall," Obama said.

The comment triggered applause from the Berlin crowd and was clearly understood as a rebuke to Trump, who put the idea of building a wall along the Mexican border at the center of his election campaign.

Unlike Obama, Trump isn't well-liked in Germany. According to an opinion poll conducted in January, only 12 percent of Germans thought it was a good thing he had become president.

However, Merkel's event with Obama was not meant to provoke Trump, with whom the German chancellor also wants to establish good relations.

The event was scheduled before the NATO summit date had been set and Merkel made sure Trump was made aware of it weeks ago, according to German government officials and an Obama Foundation aide.

'Anchor of stability'

In September, Germany is electing a new parliament. In her bid to win a fourth term, Merkel has been pushing her image as a “safe pair of hands” during a politically tumultuous time in Europe, and big diplomatic events such as the NATO summit help burnish her credentials.

One of her great advantages over her election rival, former European Parliament President Martin Schulz, is that Schulz holds no major office apart from party chief of his Social Democrats – while Merkel can use her role as Germany's leader to burnish her reputation as a Stabilitätsanker (anchor of stability) in an unstable world, officials within her Christian Democrats (CDU) say.

For example, well-aware of the power of those images, Merkel recently pushed Saudi Arabia’s King Salman on women’s rights and used a meeting with Russian President Vladimir Putin to urge him to investigate reports of persecution of gay men in Chechnya.

Thursday's event at the Brandenburg Gate was followed by standing ovations, with people applauding until Obama and Merkel walked off the stage, side by side.

Expect different images from a meeting of G20 leaders in Hamburg in July, when Trump is set to pay his first official visit to Germany.

Officials are braced for riots and mass demonstrations.

Edward-Isaac Dovere contributed reporting from Washington D.C.