Can I use AppWidgetHost
inside a Fragment
? I can't get most widgets added to my AppWidgetHost to update. Here is my scenario..
LauncherActivity.java
- Very simple, just calling setContentView with a layout.
main.xml
- Contains a couple fragments, including one that has the AppWidgetHost in it.
CodeRedWidgetHostFragment.java
onCreateView
create instances of AppWidgetHost & AppWidgetManager
create and setup my host view with a widget ID that I've stored in preferencesonStart()
appWidgetHost.startListening()onStop()
appWidgetHost.stopListening()
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?
Some widgets actually do update, like the Analog Clock for example, however, the Youtube widget doesn't auto cycle through video thumbnails.
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.
Here is the code for my Fragment class.
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 > -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);
}
}
}
}