Gov. Charlie Baker Monday signed into law a moratorium on evictions and foreclosures, making Massachusetts one of a handful of states to pass housing stability bills during the pandemic.

The moratorium, which applies to residential and small commercial tenants, will remain in effect for the next 120 days or 45 days after the COVID-19 emergency declaration has been lifted, whichever is sooner. The protections exclude evictions related to allegations of criminal activity, or those related to endangering the health and safety of others.

The new law came more than a month after Rep. Michael Connolly of Cambridge filed the bill and after the legislature passed it last week.

“It’s excellent news for everyone, particularly people who are concerned about their housing as this emergency continues,” said Connolly, reacting to the news in an interview with WGBH News.

The two-term lawmaker said he was inspired to propose the legislation after seeing housing activists raise the issue in a protest outside Boston’s housing court in March. At the time, landlords could still send eviction notices to tenants and begin the eviction process with electronic filings despite the chief justice of the state’s Housing Court ordering most eviction proceedings to a halt. The new law prohibits both actions.

“I think having it pass both branches of the legislature and be signed by the governor sends a strong statement that we are committed to maintaining housing stability and community stability as we try to endure this terrible pandemic,” Connolly said.

Across social media, supporters rejoiced at the bill's signing. Opponents, though, complained that the measure “eliminates the only way that landlords can enforce payment of rent.”

“I’m not surprised,” said Skip Schloming, president of the Small Property Owners Association. “We do have to do, realistically, with the stay at home order.”

Schloming praised two aspects of the moratorium bill: Tenants are required give their landlords documentation of financial hardship, and landlords can report missed rent payments to consumer reporting agencies if such documentation is not provided within 30 days of the missed payment.

The law directs that forms and recommendations for tenants and landlords issued by the Massachusetts Office of Housing and Economic Development document hardships due to COVID-19.