Reputation: 195
I am playing around with Room, LiveData and MVVM and I am facing an problem. I think, I might do something wrong here.
I want to retrieve LiveData and use it on main thread. I wrote an observer, that requests activity extras from database and put them to the Intent. After that I want to start the activity.
It looks like that:
Intent intent = new Intent(this, c);
if (activityDesc.isHasExtras())
{
model
.getActivityExtras(this, activityDesc)
.observe(MainActivity.this,
activityExtras -> activityExtras.forEach(e -> intent.putExtra(e.getKey(), e.getValue())));
}
startActivity(intent);
Now the activity is started, but the are too late, so the main thread started the activity, before the extras were put into the intent.
What is the proper way in this case to wait for the LiveData background process is done?
Upvotes: 0
Views: 375
Reputation: 1096
you can wait for your database work to finish then start the other activity
if (activityDesc.isHasExtras()){
model
.getActivityExtras(this, activityDesc)
.observe(MainActivity.this, activityExtras -> {
Intent intent = new Intent(MainActivity.this, c);
activityExtras.forEach(e -> intent.putExtra(e.getKey(), e.getValue())));
startActivity(intent);
}
in the meantime show some progressbar
Upvotes: 1