We construct a theoretical framework to describe the evolution of heavy flavors produced in relativistic heavy-ion collisions. The in-medium energy loss of heavy quarks is described using our modified Langevin equation that incorporates both quasi-elastic scatterings and the medium-induced gluon radiation. The space-time profiles of the fireball is described by a (2+1)-dimensional hydrodynamics simulation. A hybrid model of fragmentation and coalescence is utilized for heavy quark hadronization, after which the produced heavy mesons together with the soft hadrons produced from the bulk QGP are fed into the hadron cascade UrQMD model to simulate the subsequent hadronic interactions. We find that the medium-induced gluon radiation contributes significantly to heavy quark energy loss at high $p_\mathrm{T}$; heavy-light quark coalescence enhances heavy meson production at intermediate $p_\mathrm{T}$; and scatterings inside the hadron gas further suppress the $D$ meson $R_\mathrm{AA}$ at large $p_\mathrm{T}$ and enhance its $v_2$. Our calculations provide good descriptions of heavy meson suppression and elliptic flow observed at both the LHC and RHIC.