final Intent resultIntent = new Intent(this.getApplicationContext(), WidgetConfigure.class);
resultIntent.putExtra("actionFromUser", true);
resultIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+ resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK );
// From: http://stackoverflow.com/questions/4011178/multiple-instances-of-widget-only-updating-last-widget
final Uri data = Uri.withAppendedPath(Uri.parse("PAIN" + "://widget/id/") ,String.valueOf(appWidgetId));
resultIntent.setData(data);
- final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext());
- // Adds the back stack for the Intent (but not the Intent itself)
- stackBuilder.addParentStack(WidgetConfigure.class);
- // Adds the Intent that starts the Activity to the top of the stack
- stackBuilder.addNextIntent(resultIntent);
- final PendingIntent resultPendingIntent =
- stackBuilder.getPendingIntent(
- 0,
- PendingIntent.FLAG_UPDATE_CURRENT
- );
- remoteView.setOnClickPendingIntent(R.id.weather_appwidget, resultPendingIntent);
+// final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext());
+// // Adds the back stack for the Intent (but not the Intent itself)
+// stackBuilder.addParentStack(WidgetConfigure.class);
+// // Adds the Intent that starts the Activity to the top of the stack
+// stackBuilder.addNextIntent(resultIntent);
+// final PendingIntent resultPendingIntent =
+// stackBuilder.getPendingIntent(
+// 0,
+// PendingIntent.FLAG_UPDATE_CURRENT
+// );
+// remoteView.setOnClickPendingIntent(R.id.weather_appwidget, resultPendingIntent);
+ final PendingIntent resultPendingIntent = PendingIntent.getActivity(
+ this.getApplicationContext(),
+ 0,
+ resultIntent,
+ PendingIntent.FLAG_UPDATE_CURRENT);
+ remoteView.setOnClickPendingIntent(R.id.weather_appwidget_error, resultPendingIntent);
return remoteView;
}
final Intent resultIntent = new Intent(this.getApplicationContext(), WidgetConfigure.class);
resultIntent.putExtra("actionFromUser", true);
resultIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+ resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK );
// From: http://stackoverflow.com/questions/4011178/multiple-instances-of-widget-only-updating-last-widget
final Uri data = Uri.withAppendedPath(Uri.parse("PAIN" + "://widget/id/") ,String.valueOf(appWidgetId));
resultIntent.setData(data);
- final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext());
- // Adds the back stack for the Intent (but not the Intent itself)
- stackBuilder.addParentStack(WidgetConfigure.class);
- // Adds the Intent that starts the Activity to the top of the stack
- stackBuilder.addNextIntent(resultIntent);
- final PendingIntent resultPendingIntent =
- stackBuilder.getPendingIntent(
- 0,
- PendingIntent.FLAG_UPDATE_CURRENT
- );
+// final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext());
+// // Adds the back stack for the Intent (but not the Intent itself)
+// stackBuilder.addParentStack(WidgetConfigure.class);
+// // Adds the Intent that starts the Activity to the top of the stack
+// stackBuilder.addNextIntent(resultIntent);
+// final PendingIntent resultPendingIntent =
+// stackBuilder.getPendingIntent(
+// 0,
+// PendingIntent.FLAG_UPDATE_CURRENT
+// );
+ final PendingIntent resultPendingIntent = PendingIntent.getActivity(
+ this.getApplicationContext(),
+ 0,
+ resultIntent,
+ PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.weather_appwidget_error, resultPendingIntent);
return remoteView;
public class WidgetConfigure extends Activity {
private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
-
- final View.OnClickListener mOnClickListener = new View.OnClickListener() {
- public void onClick(View v) {
-
-
- // When the button is clicked, save the string in our prefs and return that they
- // clicked OK.
- // Push widget update to surface with newly set prefix
- final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(
- WidgetConfigure.this.getApplicationContext());
- WidgetProvider.updateAppWidget(
- WidgetConfigure.this.getApplicationContext(),
- appWidgetManager,
- mAppWidgetId);
-
- // Make sure we pass back the original appWidgetId
- final Intent resultValue = new Intent();
- resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
- WidgetConfigure.this.setResult(RESULT_OK, resultValue);
- finish();
- }
- };
+ private WidgetPreferences mPreferences;
+ private View.OnClickListener mOnClickListener;
@Override
public void onCreate(final Bundle icicle) {
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
-
+
isActionFromUser = extras.getBoolean("actionFromUser", false);
}
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
this.finish();
}
-
+
if (!isActionFromUser) {
// Set the result to CANCELED. This will cause the widget host to cancel
// out of the widget placement if they press the back button.
final Bundle args = new Bundle();
args.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
- final Fragment preferences = new WidgetPreferences();
- preferences.setRetainInstance(true);
- preferences.setArguments(args);
+ mPreferences = new WidgetPreferences();
+ // Do not retain this fragment across configuration changes because I am tired for following
+ // the fragment lifecycle (I am going to loose the instance field values but I DON'T CARE!!!)
+ mPreferences.setRetainInstance(false);
+ mPreferences.setArguments(args);
this.getFragmentManager()
.beginTransaction()
- .replace(R.id.weather_appwidget_configure_preferences, preferences)
+ .replace(R.id.weather_appwidget_configure_preferences, mPreferences)
.commit();
-
+
+ mOnClickListener = new View.OnClickListener() {
+ public void onClick(View v) {
+
+ // Save to permanent storage
+ mPreferences.onSavePreferences();
+
+ // Push widget update to surface with newly set prefix
+ final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(
+ WidgetConfigure.this.getApplicationContext());
+ WidgetProvider.updateAppWidget(
+ WidgetConfigure.this.getApplicationContext(),
+ appWidgetManager,
+ mAppWidgetId);
+
+ // Make sure we pass back the original appWidgetId
+ final Intent resultValue = new Intent();
+ resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
+ WidgetConfigure.this.setResult(RESULT_OK, resultValue);
+ finish();
+ }
+ };
// Bind the action for the save button.
this.findViewById(R.id.weather_appwidget_configure_save_button).setOnClickListener(mOnClickListener);
}
* without pain?
*/
public class WidgetPreferences extends PreferenceFragment implements OnSharedPreferenceChangeListener {
- private int appWidgetId;
+ private int mAppWidgetId;
+ private boolean mIsCountry;
+ private String mTempUnits;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- // Retain this fragment across configuration changes.
- this.setRetainInstance(true);
+ // Do not retain this fragment across configuration changes because I am tired for following
+ // the fragment lifecycle (I am going to loose the instance field values but I DON'T CARE!!!)
+ this.setRetainInstance(false);
final Bundle bundle = this.getArguments();
- appWidgetId = bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
+ mAppWidgetId = bundle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
// Load the preferences from an XML resource
this.addPreferencesFromResource(R.xml.appwidget_preferences);
/******************* Show/hide country field *******************/
String keyPreference = this.getActivity().getApplicationContext().getString(
R.string.widget_preferences_country_switch_key);
- String realKeyPreference = keyPreference + "_" + appWidgetId;
+ String realKeyPreference = keyPreference + "_" + mAppWidgetId;
// What was saved to permanent storage (or default values if it is the first time)
boolean countryValue = this.getActivity().getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE)
keyPreference = this.getActivity().getApplicationContext().getString(
R.string.widget_preferences_temperature_key);
- realKeyPreference = keyPreference + "_" + appWidgetId;
+ realKeyPreference = keyPreference + "_" + mAppWidgetId;
// What was saved to permanent storage (or default values if it is the first time)
}
@Override
- public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
+ public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
- /******************* Show/hide country field *******************/
+ /******************* Show/hide country field *******************/
String keyPreference = this.getActivity().getApplicationContext().getString(
- R.string.widget_preferences_country_switch_key);
+ R.string.widget_preferences_country_switch_key);
if (key.equals(keyPreference)) {
- final String realKeyPreference = keyPreference + "_" + appWidgetId;
- // Saving to permanent storage.
- final SharedPreferences.Editor prefs =
- this.getActivity().getSharedPreferences(
- "WIDGET_PREFERENCES",
- Context.MODE_PRIVATE).edit();
- // What is shown on the screen
- final SwitchPreference preference = (SwitchPreference) this.findPreference(key);
- if (preference.isChecked())
- {
- // Saving to permanent storage.
- prefs.putBoolean(realKeyPreference, true);
- } else {
- // Saving to permanent storage.
- prefs.putBoolean(realKeyPreference, false);
- }
- prefs.commit();
+ // What is shown on the screen
+ final SwitchPreference preference = (SwitchPreference) this.findPreference(key);
+ // Update temporal value
+ mIsCountry = preference.isChecked();
+
+ return;
}
- /********************* Temperature units **********************/
+ /********************* Temperature units **********************/
keyPreference = this.getActivity().getApplicationContext().getString(
R.string.widget_preferences_temperature_key);
if (key.equals(keyPreference)) {
- final String[] values = this.getResources().getStringArray(
- R.array.weather_preferences_temperature);
- final String[] humanValues = this.getResources().getStringArray(
- R.array.weather_preferences_temperature_human_value);
-
- // What is shown on the screen
- final ListPreference listPref = (ListPreference) this.findPreference(key);
- final String value = listPref.getValue();
- String humanValue = "";
- if (value.equals(values[0])) {
- humanValue = humanValues[0];
- } else if (value.equals(values[1])) {
- humanValue = humanValues[1];
- } else if (value.equals(values[2])) {
- humanValue = humanValues[2];
- }
- // Update data on screen
- listPref.setSummary(humanValue);
-
-
- // Saving to permanent storage.
- final String realKeyPreference = keyPreference + "_" + appWidgetId;
-
- final SharedPreferences.Editor prefs =
- this.getActivity().getSharedPreferences(
- "WIDGET_PREFERENCES",
- Context.MODE_PRIVATE).edit();
- prefs.putString(realKeyPreference, value);
- prefs.commit();
- return;
+ final String[] values = this.getResources().getStringArray(
+ R.array.weather_preferences_temperature);
+ final String[] humanValues = this.getResources().getStringArray(
+ R.array.weather_preferences_temperature_human_value);
+
+ // What is shown on the screen
+ final ListPreference listPref = (ListPreference) this.findPreference(key);
+ final String value = listPref.getValue();
+ String humanValue = "";
+ if (value.equals(values[0])) {
+ humanValue = humanValues[0];
+ } else if (value.equals(values[1])) {
+ humanValue = humanValues[1];
+ } else if (value.equals(values[2])) {
+ humanValue = humanValues[2];
+ }
+ // Update data on screen
+ listPref.setSummary(humanValue);
+
+ // Update temporal value
+ mTempUnits = value;
+
+ return;
}
-
-
-
- }
+ }
@Override
public void onResume() {
this.getPreferenceManager().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
-
+
+ public void onSavePreferences() {
+ final SharedPreferences.Editor prefs =
+ this.getActivity().getSharedPreferences(
+ "WIDGET_PREFERENCES",
+ Context.MODE_PRIVATE).edit();
+
+ /******************* Show/hide country field *******************/
+ String keyPreference = this.getActivity().getApplicationContext().getString(
+ R.string.widget_preferences_country_switch_key);
+ String realKeyPreference = keyPreference + "_" + mAppWidgetId;
+ prefs.putBoolean(realKeyPreference, mIsCountry);
+
+
+ /********************* Temperature units **********************/
+ keyPreference = this.getActivity().getApplicationContext().getString(
+ R.string.widget_preferences_temperature_key);
+ realKeyPreference = keyPreference + "_" + mAppWidgetId;
+ prefs.putString(realKeyPreference, mTempUnits);
+
+ // Saving to permanent storage.
+ prefs.commit();
+ }
+
public static void deletePreference(final Context context, final int appWidgetId) {
final String keyPreference = context.getApplicationContext().getString(
R.string.widget_preferences_temperature_key);