Create ViewModel and fragments use viewmodel like this
val viewModel: YourViewModel by activityViewModels()
in that case your viewmodel's scope is the same as activity scope.
For more information please refer to this link
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…