TAIPEI, Taiwan — Taiwan’s President Tsai Ing-wen successfully fended off a surprise primary challenge from her former premier Thursday, giving a boost to her policies to counter China going into January’s election.

Tsai’s personal popularity had been declining since her landslide election victory in 2016, and polls suggested she might have struggled to win reelection. But the primary outcome indicated support for her efforts to balance relations with China while also seeking closer relations with the United States as a counterweight to Beijing.

Tsai has rejected China’s push for unification dialogue on a possible Hong Kong-style “one country, two systems” basis. China has responded by flying military aircraft near Taiwan and trying to shrink Taiwan’s diplomatic ties around the world.

China claims sovereignty over Taiwan, where Chiang Kai-shek’s forces fled after losing to Mao Zedong’s Communists in a civil war after World War II. Since then, Taiwan has been under self-rule.

In the primary race, Tsai received an average of 35.67 percent support in landline and cellphone polls, pitting her against two potential general election challengers. Former premier William Lai received 27.48 percent.

Despite her wavering domestic popularity, Tsai’s approach with China has proved popular among Taiwanese voters and has earned the broad support of U.S. policymakers.

Lai is closely associated with a faction within the governing Democratic Progressive Party that favors more aggressive steps toward eventual formal independence.

Lai, 59, shocked Taiwan in March by entering the primary, less than two months after his January resignation as premier. The party repeatedly delayed its primary — the first in Taiwan in which an incumbent president had been challenged — to accommodate Lai’s unexpected campaign.

This gave Tsai, 62, time to “maneuver within the party” and “get her popularity back,” said Alexander C. Huang, a professor at Tamkang University’s Institute of Strategic Studies.

Had the primary been held as originally scheduled in the April time frame, Lai would probably have prevailed, Huang speculated.

Voters punished Tsai’s party in last November’s regional elections, when the opposition Kuomintang, or KMT, won three key mayoral races, including in the southern city of Kaohsiung, a longtime stronghold of Tsai’s Democratic Progressive Party.

Public confidence in Tsai wavered as her administration pushed through an unpopular slate of pension changes and was accused of mismanaging a sluggish economy.

Tsai regained momentum after responding assertively to a January speech by Chinese leader Xi Jinping, who refused to rule out the use of force to assert sovereignty over Taiwan.

On Wednesday, Tsai tweeted her support of protesters in Hong Kong opposing a controversial extradition bill, writing that “all like-minded friends in Taiwan and around the world are standing with you.”

Tsai’s new premier, Su Tseng-chang, oversaw the passage last month of Taiwan’s historic same-sex marriage law after a long period of legislative delay.

In the January election, the opposition party will probably nominate Kaohsiung Mayor Han Kuo-yu, a populist who drew thousands to a rally in Taipei this month. Han is trailed by Terry Gou, founder of tech giant Foxconn, who entered the KMT party primary in April.

Shelley Rigger, a professor of political science at Davidson College, said Tsai’s primary victory “could actually strengthen her position” heading into an election that polls had previously projected her to lose.

The controversial Han has siphoned the attention of voters due to his colorful public comments and his openness to seeking closer ties with China.

But the primary “has taken some focus off people like Han and Gou and given them the opportunity to make some mistakes,” Rigger said, allowing Tsai to renew her pitch to the voters of Taiwan.

“Taiwanese voters tend to be cautious,” said Jonathan Sullivan, director of the University of Nottingham’s China Policy Institute. Tsai, he said, represents “the best prospect for steering the line between standing up for Taiwan’s interests while avoiding actions China could decide are provocative.”