title: Android - launch configuration activity on widget click - does not work
body: <p>First, I read all similar questions and responses on xda and also google documentation. However, still I don't know why my code does not work.</p>
  
  <p>Widget code:</p>
  
  <pre><code>public class MyWidget extends AppWidgetProvider
  {
  
      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
      {
          // There may be multiple widgets active, so update all of them
          final int N = appWidgetIds.length;
          for (int i = 0; i &lt; N; i++)
          {
              int appWidgetId = appWidgetIds[i];
  
              Intent intent = new Intent(context, MyWidgetConfigureActivity.class);
              PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
  
              RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.my_widget);
              views.setOnClickPendingIntent(R.id.widget_container, pendingIntent);
  
              appWidgetManager.updateAppWidget(appWidgetId, views);
          }
      }
  
      @Override
      public void onDeleted(Context context, int[] appWidgetIds)
      {
          // When the user deletes the widget, delete the preference associated with it.
          final int N = appWidgetIds.length;
          for (int i = 0; i &lt; N; i++)
          {
              MyWidgetConfigureActivity.deleteTitlePref(context, appWidgetIds[i]);
          }
      }
  
      @Override
      public void onEnabled(Context context)
      {
          // Enter relevant functionality for when the first widget is created
      }
  
      @Override
      public void onDisabled(Context context)
      {
          // Enter relevant functionality for when the last widget is disabled
      }
  
      static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                  int appWidgetId)
      {
  
          CharSequence widgetText = MyWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
          // Construct the RemoteViews object
          RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);
          views.setTextViewText(R.id.appwidget_text, widgetText);
  
          // Instruct the widget manager to update the widget
          appWidgetManager.updateAppWidget(appWidgetId, views);
      }
  }
  </code></pre>
  
  <p>My widget layout:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:padding="@dimen/widget_margin"
                  android:background="#09C"
                  android:id="@+id/widget_container"&gt;
  
      &lt;TextView
          android:id="@+id/appwidget_text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:text="@string/appwidget_text"
          android:textColor="#ffffff"
          android:textSize="24sp"
          android:textStyle="bold|italic"
          android:layout_margin="8dp"
          android:contentDescription="@string/appwidget_text"
          android:background="#09C"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Problem - my config activity is launched only on adding widget to home screen. After I tap already added widget, nothing happens. Can you tell me what's the problem?</p>
  
  <p>[edit]
  My activity code:</p>
  
  <pre><code>package com.example.michal.widgettest2;
  
  import android.app.Activity;
  import android.appwidget.AppWidgetManager;
  import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.EditText;
  
  
  /**
   * The configuration screen for the {@link MyWidget MyWidget} AppWidget.
   */
  public class MyWidgetConfigureActivity extends Activity
  {
  
      int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
      EditText mAppWidgetText;
      private static final String PREFS_NAME = "com.example.michal.widgettest2.MyWidget";
      private static final String PREF_PREFIX_KEY = "appwidget_";
  
      public MyWidgetConfigureActivity()
      {
          super();
      }
  
      @Override
      public void onCreate(Bundle icicle)
      {
          super.onCreate(icicle);
  
          // Set the result to CANCELED.  This will cause the widget host to cancel
          // out of the widget placement if the user presses the back button.
          setResult(RESULT_CANCELED);
  
          setContentView(R.layout.my_widget_configure);
          mAppWidgetText = (EditText) findViewById(R.id.appwidget_text);
          findViewById(R.id.add_button).setOnClickListener(mOnClickListener);
  
          // Find the widget id from the intent.
          Intent intent = getIntent();
          Bundle extras = intent.getExtras();
          if (extras != null)
          {
              mAppWidgetId = extras.getInt(
                      AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
          }
  
          // If this activity was started with an intent without an app widget ID, finish with an error.
          if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
          {
              finish();
              return;
          }
  
          mAppWidgetText.setText(loadTitlePref(MyWidgetConfigureActivity.this, mAppWidgetId));
      }
  
      View.OnClickListener mOnClickListener = new View.OnClickListener()
      {
          public void onClick(View v)
          {
              final Context context = MyWidgetConfigureActivity.this;
  
              // When the button is clicked, store the string locally
              String widgetText = mAppWidgetText.getText().toString();
              saveTitlePref(context, mAppWidgetId, widgetText);
  
              // It is the responsibility of the configuration activity to update the app widget
              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
              MyWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
  
              // Make sure we pass back the original appWidgetId
              Intent resultValue = new Intent();
              resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
              setResult(RESULT_OK, resultValue);
              finish();
          }
      };
  
      // Write the prefix to the SharedPreferences object for this widget
      static void saveTitlePref(Context context, int appWidgetId, String text)
      {
          SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
          prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
          prefs.commit();
      }
  
      // Read the prefix from the SharedPreferences object for this widget.
      // If there is no preference saved, get the default from a resource
      static String loadTitlePref(Context context, int appWidgetId)
      {
          SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
          String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
          if (titleValue != null)
          {
              return titleValue;
          } else
          {
              return context.getString(R.string.appwidget_text);
          }
      }
  
      static void deleteTitlePref(Context context, int appWidgetId)
      {
          SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
          prefs.remove(PREF_PREFIX_KEY + appWidgetId);
          prefs.commit();
      }
  }
  </code></pre>
  
  <p>My activity layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="16dp"&gt;
  
      &lt;TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/configure"
          android:layout_marginBottom="8dp"/&gt;
  
      &lt;EditText
          android:id="@+id/appwidget_text"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="text"/&gt;
  
      &lt;Button
          android:id="@+id/add_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/add_widget"
          android:layout_marginTop="8dp"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>