Rajeev Jayaswal
Rajeev Jayaswal

Reputation: 1501

Android reyclerview does not get updated after restoring room database

In restore screen, I restore my sqlite db (room database) file from google drive. After I go back to my main activity - the recyclerview shows the cached data instead of showing new restored db data.

I tried initialising my ViewModel again in main activity - but that did not help.

@Override
    protected void onResume() {
        super.onResume();

        setupViewModel(); // re-initialising my ViewModel
        Toast.makeText(this, "Your expenses have been restored", Toast.LENGTH_SHORT).show();
    }

If I restart my app after restore, I can see data from restored db on main activity.

Any help would be appreciated. Workaround that I have now is - I can ask user to restart the app after they restore db from google drive.

updates:

private void setupViewModel() {
        //Associate ViewModel to this activity
        ItemViewModel itemViewModel = new ViewModelProvider(this).get(ItemViewModel.class);
        itemViewModel.getItems().observe(this, new Observer<List<Item>>() {
            @Override
            public void onChanged(List<Item> items) {
                ItemsActivity.this.items = items;
                itemsAdaptor.setItems(items);
            }
        });
    }

Upvotes: 0

Views: 210

Answers (1)

Rajeev Jayaswal
Rajeev Jayaswal

Reputation: 1501

Restarting the app is solving issue for my usecase.

I am using following code to restart the app:

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(i);
System.exit(0);

Closing and re-opening the db should ideally work but is not working for me. Once the app is re-started, it is using the new restored db.

Upvotes: 1

Related Questions