What you are trying to do works for an Activity but not for a BroadcastReceiver. An AppWidgetProvider is a BroadcastReceiver and doesn't have the methods you are trying to override (onSaveInstanceState, onRestoreInstanceState). If you add the @Override annotation to the methods it tells you
The method xyz() of type YourWidgetProvider must override or implement a supertype method
That's because the AppWidgetProvider class doesn't have a method onSaveInstanceState or onRestoreInstanceState and you can't override what's not there. Just removing the @Override annotation will get rid of the compile error but you'll just add a method that is never called (unless you do it yourself of course but that's pointless).
The way to solve this problem is to listen for configuration changes in the Application and broadcast an ACTION_APPWIDGET_UPDATE to the widget.
Add an Application class to your app
public class MyApplication extends Application {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// create intent to update all instances of the widget
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider.class);
// retrieve all appWidgetIds for the widget & put it into the Intent
AppWidgetManager appWidgetMgr = AppWidgetManager.getInstance(this);
ComponentName cm = new ComponentName(this, MyWidgetProvider.class);
int[] appWidgetIds = appWidgetMgr.getAppWidgetIds(cm);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
// update the widget
sendBroadcast(intent);
}
}
The method will be called whenever a configuration change occurs, one of them being an orientation change. You could improve the method by doing the broadcast only when it's an orientation change and not e.g. a change of the system language. The method basically sends an update broadcast to all instances of your widget (replace MyWidgetProvider by whatever name you use for your widget provider).
Define the MyApplication in your manifest
<application
android:name="yourpackagename.MyApplication"
android:description="@string/app_name"
android:label="@string/app_name"
android:icon="@drawable/app_icon">
<!-- here go your Activity definitions -->
</application>
Important this is only necessary if you use different layouts for landscape and portrait. It's NOT necessary otherwise because Android takes care of saving and restoring the widget's ui state. If you get a widget with default values then you are doing something wrong in your onUpdate method. Now I had a look at your onUpdate method and there's one major issue (there are others but I'll point out only the one that will definitely break your widget).
You create new RemoteViews after you already called the updateAppWidget with the initialized TextViews:
appWidgetManager.updateAppWidget( watchWidget, remoteViews );
//Show Prefs screen
Intent intent = new Intent(context, preferences.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// this line gets rid of all the work you have done above
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setOnClickPendingIntent(R.id.LinearLayout01, pendingIntent);
appWidgetManager.updateAppWidget( watchWidget, views );
So after creating the views and setting the values you throw it all away and create new RemoteViews which obviously have empty/uninitialized TextViews. It's also unnecessary to call the appWidgetManager.updateAppWidget twice, once per onUpdate call is enough. So what you do is this:
remoteViews.setTextViewText( R.id.widget_cigsnot, ""+ finalNotSmoked);
SfinalMoney = "$"+ finalMoney;
SfinalNS = ""+ finalNotSmoked;
SfinalDays = ""+ resultDays;
//Show Prefs screen
Intent intent = new Intent(context, preferences.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.LinearLayout01, pendingIntent);
appWidgetManager.updateAppWidget( appWidgetIds, remoteViews );
Please note that I'm using updateAppWidget(int[] appWidgetIds, RemoteViews views) instead of updateAppWidget(ComponentName provider, RemoteViews views) because that allows you to update just certain widget instances and you don't need the ComponentName which saves you another line of code.