简体   繁体   中英

How to get an Instance of ViewModel in activity in 2020/21?

I am new to the mvvm pattern. I created a ViewModel for the main activity. Now I want to get an instance of the ViewModel in the main activity.

Most Tutorials and answers here on Stackoverflow suggest using ViewModelProviders.of(... , but this is depreceated.

So according to this question on stackoverflow: ViewModelProviders is deprecated in 1.1.0 main activity in onCreate, I do the following (and I could swear I already had it running): mainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);

However, I am getting an error telling me, that no suitable constructor has been found.

error: no suitable constructor found for ViewModelProvider(MainActivity)

Alternatively to make absolutely clear, that the MainActivity shall be the ViewModelStoreOwner, I created a variable ViewModelStoreOwner vmso = this; and put that variable into the constructor like so: mainActivityViewModel = new ViewModelProvider(vmso).get(MainActivityViewModel.class);

You should update your gradle file to:

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

And due to this change you can pass Activity to the constructor you mentioned:

mainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);

Using Fragment-ktx libr in your app you can get viewModel as below

First Update Gradle File as app -> build.gradle

implementation 'androidx.fragment:fragment-ktx:1.1.0'

// get ViewModel in Activity or Fragment as

private val viewModel: MainActivityViewModel by viewModels()

// If you want to get same instance of ViewModel in ChildFragment as

 private val viewModel: MainActivityViewModel by viewModels(
    ownerProducer = { requireParentFragment() }
)

Use val viewModel by viewModels<TheViewModel>() in Activities and val viewModel by activityViewModels<TheViewModel>() in fragment to obtain the same viewmodel from the activity (so sharing the viewmodel).

This is part of androidx now

For now, the only thing working for me was to use: MainActivityViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainActivityViewModel.class);

However, I am still thankful for any advice, how this can be done different using new ViewModelProvider(this).get(MainActivityViewModel.class);

Get ViewModel In New Way

MainActivityViewModel mainActivityViewModel;
    mainActivityViewModel= new ViewModelProvider(this).get(MainActivityViewModel.class);

Duplicate question and already answered here

Simply replace:

This:

boardViewModel = ViewModelProviders.of(this).get(BoardViewModel::class.java)

With this:

boardViewModel = ViewModelProvider(this).get(BoardViewModel::class.java)

You can use a ViewModelFactory :

val viewModelFactory = VMFactory(requireActivity().application)
viewModel= ViewModelProvider(requireActivity(),viewModelFactory).get(MainViewModel::class.java)

VMFactory code:

class VMFactory(application: Application) : ViewModelProvider.NewInstanceFactory() {

    val _application: Application=application

    @NonNull
    override fun <T : ViewModel?> create(@NonNull modelClass: Class<T>): T {
            return  MainViewModel(_application) as T
    }
}

Please note that here my MainViewModel extends AndroidViewModel and hence requires application as the input parameter.

I had similar problems and in the end found out I missed off the " extends ViewModel " within the class definition, so it should look like this:

public class ViewModelClass extends ViewModel
{
    // Tracks the score for Team A
    public int scoreTeamA = 0;

    // Tracks the score for Team B
    public int scoreTeamB = 0;
}

ViewModel viewModel = ViewModelProviders.of(this).get(ViewModel.class);

Android Support library version is depreciated. So you need to make sure that you import Androidx. And you need to implement androidx dependency in Gradle file.

It is not advised to create a new ViewModel object with "new" keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM