title: AppWidgetHost not updating widgets
body: <p>Can I use <code>AppWidgetHost</code> inside a <code>Fragment</code>?  I can't get most widgets added to my AppWidgetHost to update.  Here is my scenario..</p>
  
  <p><strong>LauncherActivity.java</strong>   </p>
  
  <ul>
  <li>Very simple, just calling setContentView with a layout.</li>
  </ul>
  
  <p><strong>main.xml</strong>  </p>
  
  <ul>
  <li>Contains a couple fragments, including one that has the AppWidgetHost in it.</li>
  </ul>
  
  <p><strong>CodeRedWidgetHostFragment.java</strong>  </p>
  
  <ul>
  <li><p><code>onCreateView</code><br>
   create instances of AppWidgetHost &amp; AppWidgetManager<br>
   create and setup my host view with a widget ID that I've stored in preferences  </p></li>
  <li><p><code>onStart()</code><br>
   appWidgetHost.startListening()</p></li>
  <li><p><code>onStop()</code><br>
   appWidgetHost.stopListening()  </p></li>
  </ul>
  
  <p>When I get my instances of AppWidgetHost and AppWidgetManager I'm using getActivity() to get the Fragments hosting activity.  I'm wondering if this is why my widgets aren't updating?</p>
  
  <p>Some widgets actually do update, like the Analog Clock for example, however, the Youtube widget doesn't auto cycle through video thumbnails.  </p>
  
  <p>I should mention that I select the widget I want in my AppWidgetHost in another activity that stores the selected widget's id in SharedPreferences.</p>
  
  <p>Here is the code for my Fragment class.</p>
  
  <pre><code>package com.brockoli.android.codered.widgethost;
  
  import android.app.Fragment;
  import android.appwidget.AppWidgetHost;
  import android.appwidget.AppWidgetHostView;
  import android.appwidget.AppWidgetManager;
  import android.appwidget.AppWidgetProviderInfo;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.brockoli.android.codered.R;
  
  public class CodeRedWidgetHostFragment extends Fragment {
  private static final String TAG = CodeRedWidgetHostFragment.class.getSimpleName();
  
  public static final String CODERED_WIDGET_ID = "CODERED_WIDGET_ID";
  
  private SharedPreferences mSharedPrefsWidgets;
  
  AppWidgetManager mAppWidgetManager;
  AppWidgetHost mAppWidgetHost;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      View parentView = inflater.inflate(R.layout.widget_host, null);
  
      mSharedPrefsWidgets = getActivity().getSharedPreferences("codered_widgets", 0);
  
      mAppWidgetManager = AppWidgetManager.getInstance(getActivity());
      mAppWidgetHost = new AppWidgetHost(getActivity(), R.id.APPWIDGET_HOST_ID);
  
      createWidget(parentView);
  
      return parentView;
  }
  
  @Override
  public void onStart() {
      super.onStart();
      mAppWidgetHost.startListening();
  }
  
  @Override
  public void onStop() {
      super.onStop();
      mAppWidgetHost.stopListening();
  }
  
  
  @Override
  public void onResume() {
      super.onResume();
      createWidget(getView());
  }
  
  /**
   * Creates the widget and adds to our view layout.
   */
  public void createWidget(View v) {
      if (v != null) {
          // Remove any existing widgets from the widget host
          ((ViewGroup) v).removeAllViews();
          int appWidgetId = mSharedPrefsWidgets.getInt(CODERED_WIDGET_ID, -1);
          if (appWidgetId &gt; -1) {
              AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
              AppWidgetHostView hostView = mAppWidgetHost.createView(getActivity(), appWidgetId, appWidgetInfo);
              hostView.setAppWidget(appWidgetId, appWidgetInfo);
              ((ViewGroup) v).addView(hostView);
              Log.i(TAG, "The widget size is: " + appWidgetInfo.minWidth + "*" + appWidgetInfo.minHeight);
          }   
      }
  }
  }
  </code></pre>