Fix a bug that the app crashes when transition to background.