A
A
Alexander Sinitsyn2020-04-14 15:49:58
Android
Alexander Sinitsyn, 2020-04-14 15:49:58

Why does the constructor fire every time the ViewModel is connected?

Either I missed something, or ...

I decided to try, I created the class SharedModel extends ViewModel
Inside a couple of livdata fields and a few simple ones, like

private int mGameMode;
    private int mGameLevel;


Then I call in the activity and fragments
mSharedModel = new ViewModelProvider(this).get(SharedModel.class);


In one fragment I set the mGameMode field, in the second one mGameLevel, in the third one I turn ... they have zeros. Brought to the log, wherever the set is made, the constructor is called. It should exist outside of fragments and activities... What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
orbit070, 2020-04-14
@a_u_sinitsin

Because in fragments, instead of this, you need to pass getactivity ()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question