Sam
Sam

Reputation: 95

Android - Widget Disappears immediately when placed

I'm trying to add a widget into my app and I am facing a problem. Whenever I grab the widget and place it on my homescreen, it just disappears. I can only see it for about 0.5 seconds. Here is the code:

Manifest.xml

<receiver
        android:label="MyApp"
        android:name=".widget.MyWidgetProvider" >
   <intent-filter >
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
   </intent-filter>
    <meta-data
      android:name="android.appwidget.provider"
      android:resource="@xml/widget_info" />
</receiver> 

MyWidgetProvider.java

public class MyWidgetProvider extends AppWidgetProvider {


     @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    //IDs
    for (int i=0; i<appWidgetIds.length; i++) {
        int currentWidgetId = appWidgetIds[i];
      // Create data
        SharedPreferences prefs = context.getSharedPreferences("com.example.app_preferences", 0);
      int number = prefs.getInt("progress", 0);

      RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
          R.layout.appwidget);
      Log.w("WidgetExample", String.valueOf(number));

      // Set the text
      remoteViews.setTextViewText(R.id.widgettext, String.valueOf(number));

      // Register an onClickListener
      Intent intent = new Intent(context, MyWidgetProvider.class);

      intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

      PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
          0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

      remoteViews.setOnClickPendingIntent(R.id.widgettext, pendingIntent);
      appWidgetManager.updateAppWidget(currentWidgetId, remoteViews);
      Toast.makeText(context, "widget added", Toast.LENGTH_SHORT).show();

    }
  }
}

appwidget.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/widgetlayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/widgetimage" >

    <TextView
        android:id="@+id/widgettext"
        style="@android:style/TextAppearance.Medium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

widget_info.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:updatePeriodMillis="86400000"
android:minWidth="120dp"
android:minHeight="50dp"
android:initialLayout="@layout/appwidget"
android:previewImage="@drawable/widgetimage"
android:configure="com.example.app.widget.MyWidgetProvider" >
</appwidget-provider>

you can see the last line of the java file contains a toast. This toast actually gets called and there are no errors whatsoever in the logcat.

Thanks!

Upvotes: 0

Views: 1329

Answers (2)

rothloup
rothloup

Reputation: 1290

Another possibility other than 3c71's answer is that you might have forgotten to use the two-parameter version of setResult(int resultCode, Intent data). The data parameter has to include an EXTRA which identifies the widget ID.

Intent intent = new Intent();
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, intent);
sendBroadcast(intent);

The widget ID is provided to the configuration activity as an EXTRA in the Intent that is supplied to the activity when it is called. Retrieve it and store it like this:

mAppWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,     AppWidgetManager.INVALID_APPWIDGET_ID);

Upvotes: 0

3c71
3c71

Reputation: 4511

You're android:configure element must refer to an activity that will allow configuration of your widget. This activity must set result to RESULT_OK, otherwise configuration is cancelled and widget is removed.

I suppose referring to your widget provider, not an activity, has the same effect of returning RESULT_CANCEL from the configuration activity.

If you don't have a configuration activity, simply remove this entry and the widget will be added without trying to open such.

Upvotes: 1

Related Questions