Ask Your Question
1

When a fragment is destroyed, why isn't the Android ViewModel also destroyed?

asked 2023-05-11 01:14:09 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-11 01:25:01 +0000

ladyg gravatar image

The Android ViewModel is not directly tied to the Fragment's lifecycle. The ViewModel is instead tied to the activity or the process, depending on how it was created. When a Fragment is destroyed and recreated due to a configuration change, such as a screen rotation, the same ViewModel instance is retained and reattached to the new Fragment instance. This helps to prevent data loss and ensure that the ViewModel can continue to provide data and state management for the UI components even after configuration changes occur.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-11 01:14:09 +0000

Seen: 16 times

Last updated: May 11 '23