Fixed a bug when leaving the view