The action for the gravitational field is known as the Einstein-Hilbert action: $$\begin{equation} S_{G}=\int d^4 x \sqrt{|g|} R \end{equation}$$ where $R$ is the Ricci scalar. The action for a matter field that is minimally coupled to gravity is: $$\begin{equation} S_{matter}=\int d^4 x \sqrt{|g|} \mathcal{L_{matter}}. \end{equation}$$
Also, we can write the total action as $$\begin{equation} S_{tot}=S_{G}+S_{matter}=\int d^4 x \sqrt{|g|} [R+\mathcal{L_{matter}}]~, \end{equation}$$ which can be used to derive the Einstein's field equations by varying this action with respect to the metric.
Varying $S_{matter}$ with respect to $\phi$ will give the equation of motion of the field; varying $S_{tot}$ with respect to $\phi$ will give the same result of the equation of motion of the field, because $R$ is independent of $\phi$. However, when it comes to the Hamiltonian, the results are different. Starting from $S_{matter}$, we get $$\begin{equation} H_{matter}=\int d^4 x \sqrt{|g|} [\frac{\partial\mathcal{L_{matter}}}{\partial \dot\phi}~~\dot{\phi}-\mathcal{L_{matter}}], \end{equation}$$ however, starting from $S_{tot}$ we get $$\begin{equation} H_{matter}=\int d^4 x \sqrt{|g|} [\frac{\partial\mathcal{L_{matter}}}{\partial \dot\phi}~~\dot{\phi}-(\mathcal{L_{matter}}+R)], \end{equation}$$.
The two $H_{matter}$ are different by the term $R$. So, which $H_{matter}~$ is correct?