Manually clearing an Android ViewModel?

后端 未结 12 1043
独厮守ぢ
独厮守ぢ 2020-12-04 18:50

Edit: This question is a bit out of date now that Google has given us the ability to scope ViewModel to navigation graphs. The better approach (rather

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 19:18

    As I know you can't remove ViewModel object manually by program, but you can clear data that stored in that,for this case you should call Oncleared() method manually for doing this:

    1. Override Oncleared() method in that class that is extended from ViewModel class
    2. In this method you can clean data by making null the field that you store data in it
    3. Call this method when you want clear data completely.

提交回复
热议问题