i wanted to access Room DB inside my widget class to update the widget according to the stored data.
the problem here is the ViewModelProvider require a fragment or activity to work with..
ViewModelProviders.of(fragment).get(YourViewModel.class);
which is not available in the app widget class so how to do it?
So answering my self here..
the only way that worked is to use the context to get the DB instance from the abstract class and run the DAO queries directly in background thread without the ViewModelProviders class
new AsyncTask<Context, Void, List<Data>>(){ @Override protected List<Quote> doInBackground(Context... context) { List<Data> List=null; AppDatabase db = AppDatabase.getDatabase(context[0]); List = db.DataModel().getData(); return List; } @Override protected void onPostExecute(List<Quote> List) { super.onPostExecute(List); if(List != null){ final Random random=new Random(); for (int appWidgetId : appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId, quoteList.get(random.nextInt(List.size()))); } } } }.execute(context); }