title: Cant get SharedPreferences for a widget
body: <p>I want to develop a widget... But i have a problem when passing strings to the widget from AppWidgetProvider...I am doing this through shared preferences</p>
  
  <p>The problem comes when I store a variable in SharedPreferences, when I try to obtain its value, it returns not a null value but something like this: "" </p>
  
  <p>This value is returned although it exists that variable in WidgetPrefs.xml, i thimk there is an error with prefs variable, but i am not sure</p>
  
  <p>This is the code of WidgetConfig:</p>
  
  <pre><code>public class ActivityWidgetConfig extends Activity implements OnClickListener{
  
  private int widgetId = 0;
  
  private String titlestring;
  private String textstring;
  
  private EditText etTitle;
  private EditText etText;
  
  private ImageButton btnTitle;
  private ImageButton btnText;
  
  private ImageView previewIcon;
  
  private Button iconButton;
  private Button createButton;
  
  private int icon_id;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_widgetconfig);
  
      Intent intentOrigen = getIntent();
      Bundle params = intentOrigen.getExtras();
  
      widgetId = params.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
  
      setResult(RESULT_CANCELED);
  
      etTitle = (EditText) findViewById(R.id.etTitle);
      etText = (EditText) findViewById(R.id.etText);
      btnTitle = (ImageButton) findViewById(R.id.btnTitle);
      btnText = (ImageButton) findViewById(R.id.btnText);
      createButton = (Button)findViewById(R.id.createButton);
      iconButton = (Button)findViewById(R.id.iconButton);
  
      btnTitle.setOnClickListener(this);
      btnText.setOnClickListener(this);
      createButton.setOnClickListener(this);
      iconButton.setOnClickListener(this);
  
      SharedPreferences prefs = getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
      titlestring = prefs.getString("widgettitle", "");
      textstring = prefs.getString("widgettext", "");
      icon_id = prefs.getInt("widgeticon_id", android.R.drawable.stat_sys_warning);
  
      etTitle.setText(titlestring);
      etText.setText(textstring);
  
      previewIcon = (ImageView) findViewById(R.id.previewIcon);
      previewIcon.setImageResource(icon_id);
  
  }
  
  @Override
  public void onClick(View v) {
      SharedPreferences prefs = getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
      Editor editor = prefs.edit();
      switch (v.getId()){
          case R.id.btnTitle:
              editor.remove("widgettitle");
              editor.commit();
              etTitle.setText("");
              break;
          case R.id.btnText:
              editor.remove("widgettext");
              editor.commit();
              etText.setText("");
              break;
          case R.id.createButton:
              editor.putString("widgettitle", titlestring);  
              final String textstring=etText.getText().toString();
              editor.putString("widgettext", textstring);
              editor.putInt("widgeticon_id", icon_id);
              editor.commit();
  
              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ActivityWidgetConfig.this);
              WidgetOne.updateWidget(ActivityWidgetConfig.this, appWidgetManager, widgetId);
              Intent resultado = new Intent();
              resultado.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
              setResult(RESULT_OK, resultado);
              finish();
              break;
          case R.id.iconButton:
              Intent i = new Intent(this, ActivityIcons.class); 
              startActivityForResult(i, 1);
              break;
      }
  }
   }
  </code></pre>
  
  <p>And this is the code of the widget:</p>
  
  <pre><code>public class WidgetOne extends AppWidgetProvider {
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  
          for (int i = 0; i &lt; appWidgetIds.length; i++) 
          {
              //ID del widget actual
              int widgetId = appWidgetIds[i];
  
              //Actualizamos el widget actual
              //updateWidget(context, appWidgetManager, widgetId);
          }
  
  }
  
  
  @Override
  public void onReceive(Context context, Intent intent) {
  
      if (intent.getAction().equals("com.iamaner.ACTUALIZAR_WIDGET")) {
  
  //          //Obtenemos el widget manager y la lista de IDs de nuestro widget
  //          ComponentName wgt = 
  //              new ComponentName(context, MiWidget.class);
  //          
  //          AppWidgetManager widgetManager =
  //              AppWidgetManager.getInstance(context);
  //          
  //          int[] appWidgetIds = widgetManager.getAppWidgetIds(wgt);
  //          
  //          //Actualizamos todos los widgets
  //          for(int i=0; i&lt;appWidgetIds.length; i++)
  //              actualizarWidget(context, widgetManager, appWidgetIds[i]);
  
          //Obtenemos el ID del widget a actualizar
          int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
  
          //Obtenemos el widget manager de nuestro contexto
          AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
  
          //Actualizamos el widget
          //if (widgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
          //  actualizarWidget(context, widgetManager, widgetId);
          //}
      }
  
      super.onReceive(context, intent);
  }
  
  public static void updateWidget(Context context, AppWidgetManager appWidgetManager, int widgetId)
  {
      SharedPreferences prefs = context.getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
      RemoteViews controles = new RemoteViews(context.getPackageName(), R.layout.widget_1);
  
      //Asociamos los 'eventos' al widget
      Intent intent = new Intent("com.iamaner.ACTUALIZAR_WIDGET");
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
  
      controles.setTextViewText(R.id.txtMensaje, prefs.getString("widgettitle", ""));
      controles.setTextViewText(R.id.txtMensaje, prefs.getString("widgetext", ""));
  
      controles.setImageViewResource(R.id.ivIcon, icon_id);
  
      appWidgetManager.updateAppWidget(widgetId, controles);
  }
  
  }
  </code></pre>