Quick explanation of the app. It is a simple widget where 4 values are saved via SharedPreferences. The only activity view is the place where the preferences are set. Once done, the variables are saved, and the widget is updated. The widget updates on it's own every 90,000ms. This is all working fine. The problem I have is when the orientation changes. When that happens, the widget is destroyed, and then remade. What I get is the raw XML with the variable Text fields set to default. It stays like this until the next manual or automatic update.
EDIT: This question is now altered considerably. The two responses that I got pointed out a few things that were wrong. Read all the comments to see all of that. After the last attempt, which was to try implenting a tag in the in the manifest, I got even worse results. The program simply failed to run at all.
I'm going with the last thing said in the same answer that provided that idea. There must be something wrong with my onUpdate(). Here is the code for the entire class:
public class WatchWidget extends AppWidgetProvider
{
public void onCreate(Context context, Bundle savedInstanceState)
{
RemoteViews remoteViews;
remoteViews = new RemoteViews( context.getPackageName(), R.layout.main );
remoteViews.setTextViewText( R.id.widget_elapsedtime, "Time");
}
@Override
public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews remoteViews;
ComponentName watchWidget;
DateFormat format = SimpleDateFormat.getTimeInstance( SimpleDateFormat.MEDIUM, Locale.getDefault() );
remoteViews = new RemoteViews( context.getPackageName(), R.layout.main );
watchWidget = new ComponentName( context, WatchWidget.class );
//remoteViews.setTextViewText( R.id.widget_textview, "" + format.format( new Date()));
//widget_date
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = df.format(c.getTime());
//remoteViews.setTextViewText( R.id.widget_date, formattedDate);
SharedPreferences prefs = context.getSharedPreferences("qsPrefs", context.MODE_PRIVATE);
String sdayTextEdit = prefs.getString("dayTextEdit", "23");
//String sdayTextEdit ="23";
String smonthTextEdit = prefs.getString("monthTextEdit", "3");
String syearTextEdit = prefs.getString("yearTextEdit", "2014");
String scostTextEdit = prefs.getString("costTextEdit", "8.5");
String spacksTextEdit = prefs.getString("packsTextEdit", ".95");
//Starting day
Calendar pastdate = Calendar.getInstance();
//int inputDay = 25;
int inputDay = Integer.valueOf(sdayTextEdit);
int inputMonth = 2; //starts at 0
inputMonth = Integer.valueOf(smonthTextEdit);
int inputMonthAdj = inputMonth-1;
int inputYear = 2014;
inputYear = Integer.valueOf(syearTextEdit);
pastdate.set(Calendar.DAY_OF_MONTH, inputDay);
pastdate.set(Calendar.MONTH, inputMonthAdj);
pastdate.set(Calendar.YEAR, inputYear);
double packsPer = .95;
packsPer = Double.parseDouble(spacksTextEdit.toString());
double perPack = 8.57;
perPack = Double.parseDouble(scostTextEdit.toString());
inputMonthAdj = inputMonth;
remoteViews.setTextViewText( R.id.widget_date, "From: " + inputMonthAdj +"/"+ inputDay +"/"+ inputYear );
SfinalDate = "From: " + inputMonthAdj +"/"+ inputDay +"/"+ inputYear;
Calendar today = Calendar.getInstance();
DecimalFormat dform = new DecimalFormat("0.00");
long dateDiff = today.getTimeInMillis() - pastdate.getTimeInMillis();
long resultDays = dateDiff / (24*60*60*1000);
remoteViews.setTextViewText( R.id.widget_elapsedtime, ""+ resultDays);
double savedmoney = packsPer * perPack * resultDays;
String finalMoney = dform.format(savedmoney);
remoteViews.setTextViewText( R.id.widget_savedmoney, "$"+ finalMoney);
double notSmoked = resultDays * 20 * packsPer;
int finalNotSmoked = (int) Math.round(notSmoked);
remoteViews.setTextViewText( R.id.widget_cigsnot, ""+ finalNotSmoked);
SfinalMoney = "$"+ finalMoney;
SfinalNS = ""+ finalNotSmoked;
SfinalDays = ""+ resultDays;
appWidgetManager.updateAppWidget( watchWidget, remoteViews );
//Show Prefs screen
Intent intent = new Intent(context, preferences.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setOnClickPendingIntent(R.id.LinearLayout01, pendingIntent);
appWidgetManager.updateAppWidget( watchWidget, views );
}
}
Having posted that... There may be a few extra lines attempting to set things that are no longer relavent. But the code as it is there works. The onUpdate() fires fine at the automatic interval (90000ms). And the activity that launches to set the preferences, also makes the call when it closes, which also updates it fine. This is what has led me to believe that the entire void must be ok. But there must be something amiss. Because when the screen orientation changes, the onUpdate() does not run. And, again, as per the second answer, it shouldn't have to anyway, because the system is supposed to retain the UI on it's own. But it's not.