I've solved it something like this: created two methods with SharedPreferences, where I put flags of what method is executes now:
private void setWidgetActive(Context context, boolean active,String field){
Context appContext = context.getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(field, active);
edit.commit();
}
private boolean getWidgetActive(Context context, String field)
{
Context appContext = context.getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
return prefs.getBoolean(field, false);
}
then, my onEnabled method:
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
setWidgetActive(context,true, WIDGET_ACTIVE);
}
in onUpdate method:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
setWidgetActive(context,true, WIDGET_UPDATE);
if (getWidgetActive(context, WIDGET_UPDATE) & !getWidgetActive(context, WIDGET_ACTIVE))
{
myString = context.getResources().getStringArray(R.array.quotes);
q = myString[rgenerator.nextInt(myString.length)];
mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
saveQuote(context, q, mydate);
}
else
{
ArrayList<Quote> importList = getQuotes(context);
q = importList.get(importList.size() - 1).getText();
mydate = importList.get(importList.size() - 1).getDate();
setWidgetActive(context,false, WIDGET_ACTIVE);
}
}
so if widget was added just now, getWidgetActive(context, WIDGET_ACTIVE) returns true and I get last added field of database and send it to the TextView of the widget. And in that case, when defined time has passed, I get new random stting from array and save in database and also send to the TextView.