The US President has called on African nations to treat gays and lesbians equally under the law.
On his first presidential visit to Kenya, his father's birthplace, Barack Obama was asked about gay rights during a joint news conference with his Kenyan counterpart, Uhuru Kenyatta.
Mr Obama said he is "painfully aware of the history when people are treated differently under the law" - drawing on his own background as an African-American in the US.
"That's the path whereby freedoms begin to erode and bad things happen," Mr Obama said.
"When a government gets in the habit of treating people differently, those habits can spread."
Gay sex is a crime in Kenya and is punishable by up to 14 years in jail.
Mr Kenyatta told reporters that while the US and Kenya do agree on a lot, there are certain things that some cultures just do not accept.
He added that gay rights "is not really an issue on the foremost mind of Kenyans. And that is a fact."