By Jonathan Cheng

BEIJING--An official gauge of China's factory activity expanded at a faster pace in July, as improving demand inside and outside the country kept the recovery of the world's second-largest economy on track.

China's official manufacturing purchasing managers index rose to 51.1 in July from 50.9 in June, the National Bureau of Statistics said Friday, beating economists' expectations and marking the fifth consecutive month that the closely watched measure of China's factory activity topped the 50 mark that separates expansion from contraction.

China's official nonmanufacturing purchasing managers index, a gauge of business activity outside the factory floor, remained in positive territory, thanks to robust activity in the property and investment sectors, fueling construction activity.

Even so, the overall nonmanufacturing index slipped to 54.2 in July, compared with 54.4 in June, the statistics bureau said, indicating a slight deceleration in the recovery for China's service sector as heavy floods hit swaths of central and southern China.

Taken together, the data suggests that consumer demand continues to lag behind the recovery in China's industrial capacity, which has recovered more quickly from the coronavirus.

"It's still a two-track recovery," said Andrew Polk, a partner at research firm Trivium China. Mr. Polk said that while recent data points show China's factories have returned to pre-coronavirus levels, consumer demand remains much weaker--which means inventory is piling up.

"It's good in the short term, but it's building in headwinds to growth in the third and fourth quarter," he said. "Industry later will have to back off to draw down its inventory." He added, "It's still far off from what we'd call a normal recovery."

On the manufacturing side, subindexes for both production and new orders grew at a faster pace in July, thanks in large part to a substantial improvement in new export orders--though the measure of overseas demand still remained in contraction territory.

"Friday's data showed China's recovery is moving forward with continued supply-side resumptions, while demand still lagged behind," said Ding Shuang, an economist at Standard Chartered.

The pandemic's continued toll on Western countries that are struggling to contain the virus, including the U.S., has been a double-edged sword for China's economy. While it has sapped overseas demand, it has also opened an opportunity for China's factories to fill the void, Mr. Ding said.

China's strong factory bounceback helped drive China's overall economy to a 3.2% expansion in second-quarter gross domestic product from a year earlier, after it suffered a historic 6.8% contraction in the first three months of the year.

On Monday, China reported that profits at its large industrial companies rose by double-digit percentage points from a year earlier in June. Earlier this month, China posted a surprise increase in exports for June, rising 0.5% from a year earlier.

With the recovery largely on track, China's top policy makers signaled Thursday that they wouldn't further ramp up stimulus measures that could exacerbate financial risks and add to mounting debt worries, a lesson Beijing learned after launching a huge stimulus program to counter the 2008 financial crisis.

Chinese leaders seemed to take note of these challenges by pledging again in Thursday's meeting to give priority to spurring on the domestic economy, while encouraging the domestic and export economies to feed off each other.

Economists have argued that it is still too early for China to roll back stimulus measures, given the uneven nature of the government-supported recovery and continued external headwinds, including the worsening pandemic in some parts of the world and rising U.S.-China geopolitical tensions.

Grace Zhu contributed to this article.

Write to Jonathan Cheng at jonathan.cheng@wsj.com