0

I have this database that contains information about celebrities, lots of information, ie movies, roles, articles...

Without the viewmodel, all of the stuff in the edit text gets lost. So i dont want to lose all of that info on rotation.

So i send an intent with all the relevant info of a selected celebrity(sql room) to the addeditactivity, so now how do i initialize the viewmodel? If I do getStringExtra in onCreate, wouldn't that just rewrite the viewmodel again when the activity gets recreated?

how do i get around this, also is there a better alternative? im a beginner, thanks in advance!

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   // ......... shared preferences code .................
   // get intent code ................

   mViewModel = ViewModelProviders.of(this).get(testViewModel.class);
   mViewModel.celebName = intent.getStringExtra(CELEB_NAME);
   // similar..........
  
}
Absolute
  • 11
  • 2

2 Answers2

0

You can initialize your viewModel in onCreate() using ViewModelFactory to pass your data from getStringExtra to your viewModel. Use viewModelFactory pattern to pass data to your viewModel. Here "YourINFOParameter" is your data from getStringExtra. So after initialization in your viewModel you have set parameter you can observe using liveData. I am suing Kotlin in answer.

as example:

override fun onCreateView(
........
     val viewModelFactory =
            TestViewModelFactory(
                yourINFOparameter,
                application)

val testViewModel = ViewModelProvider(
            this, viewModelFactory
        ).get(TestViewModel::class.java)
.................

Then declare class TestViewModelFactory:

class TestViewModelFactory(
private val yourINFOparameter: String, private val application: Application) : ViewModelProvider.Factory {
    @Suppress("unchecked_cast")
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(TestViewModel::class.java)) {
            return TestViewModel(yourINFOparameter,
            application) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

And your viewModelclass:

class TestViewModel(yourINFOparameter: String, application: Application) : AndroidViewModel(application) {

............................}
Leontsev Anton
  • 727
  • 7
  • 12
0

No, ViewModels have separate lifecyles from Activities. When your Activity gets rotated/recreated it will still use your previously-instantiated ViewModel (and any data saved in it) unless it has already been cleared.

See https://developer.android.com/topic/libraries/architecture/viewmodel

If the activity is re-created, it receives the same MyViewModel instance that was created by the first activity. When the owner activity is finished, the framework calls the ViewModel objects's onCleared() method so that it can clean up resources.

Thomas Lee
  • 71
  • 1
  • 3