By Ben Eisen

Bank of America Corp. struck an optimistic tone Thursday, saying it is seeing early signs of a rebound amid an economy racked by the coronavirus.

JPMorgan Chase & Co., Citigroup Inc. and Wells Fargo & Co. played a somber note when they reported earnings earlier this week, warning that they expect the current recession to be worse than initially expected.

Bank of America CEO Brian Moynihan emphasized that spending is picking up among his customers.

"As states began to reopen in the past couple months, we saw an improvement in spending levels as customers became more active buying fuel and spending on home projects and eating out," he said on a call with analysts.

Bank of America said Thursday it set aside $5.12 billion in the second quarter to cover losses on its consumer and commercial loans. JPMorgan, Citigroup and Wells Fargo set aside between $7.9 billion and $10.47 billion each.

Banks have a unique view into the economy because they handle much of the flow of money that powers it. Yet even their executives have struggled to get a clear read on where it is headed at a time when millions of people are unemployed but the federal government is flooding the country with unprecedented stimulus.

While economic numbers have continued to look bleak, the stock market has rebounded to near record levels, leaving many people struggling to understand the dichotomy.

All of the major lenders, Bank of America included, are preparing for a wave of loans to go bad, and they made clear they are bracing for a long recession. Bank of America's second-quarter profit fell 52%.

The government stimulus measures, as well as the banks' own decisions to let troubled borrowers temporarily skip payments, has helped keep many people afloat. But those measures won't last forever, and it isn't clear that the economy will be back on track when they expire.

"I don't think anybody should leave any bank earnings call this quarter simply feeling like the worst is absolutely behind us, and it's a rosy path ahead," said Michael Corbat, Citigroup's CEO.

The economy has begun to reopen after an abrupt shutdown in the spring. But a resurgence in coronavirus cases is thwarting many reopening plans and could force another closure that strikes a second blow, economists warn.

In states like Texas and Florida, where virus cases have grown the most and local officials have reversed course on reopening, Mr. Moynihan said that spending had dropped off a bit, but was still substantially higher than during the initial shutdown. He said that the drop-off in spending from actions like bar closures was outweighed by spending on other goods like home improvement, but that the rebound appeared likely to ebb and flow.

Overall, the amounts that Bank of America customers have been putting on their credit cards and pulling out of their bank accounts were well below 2019 levels in April and May, the bank said, but about flat in June and higher in early July.

There have been other signs that consumption is rebounding. The Commerce Department said Thursday that June retail sales increased 7.5% as stores and restaurants reopened.

To be sure, Bank of America still expects deep unemployment and a yearslong rebound from the current period of contraction. The bank said it processed some 1.8 million payment deferrals on consumer debt so far this year, mostly in its credit card accounts.

The move to sock away money damped Bank of America's profit. The Charlotte, N.C.-based bank said it earned $3.53 billion, versus the $7.35 billion it made in the second quarter last year.

Per share, earnings of 37 cents beat the 28 cents that analysts polled by FactSet had forecast. Still, the lender's shares fell 2.7% in the early afternoon, underperforming the KBW Nasdaq index of bank stocks, which was flat.

Bank of America posted revenue of $22.33 billion, down about 3% from last year.

Like other banks, Bank of America reported a strong quarter in its Wall Street businesses. Traders have benefited from wild markets, and bankers have benefited from companies rushing to sell stock and debt to shore up their finances. Morgan Stanley, which focuses heavily on Wall Street businesses, reported a surge in earnings Thursday, a contrast to the banks with bigger consumer operations like Bank of America and Wells Fargo.

Bank of America's adjusted trading revenue of $4.41 billion was up 35% from a year ago. The bank posted a 50% jump in fixed-income trading revenue, as well as a roughly 7% rise in equities.

Total investment banking fees jumped 57% to a record $2.16 billion, led by an 87% rise in the business that handles equity issuance. Its businesses that help companies issue debt and advise on mergers also had strong growth.

Expenses in the second quarter increased slightly versus last year to $13.41 billion, as banks spent money setting up employees to work from home and safeguarding their branches.

Write to Ben Eisen at ben.eisen@wsj.com