Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6098501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:09:57+00:00 2026-05-23T13:09:57+00:00

I have an Android widget that has a configure activity. I also have an

  • 0

I have an Android widget that has a configure activity. I also have an ImageView “button” set up on the widget to launch the configure activity, in case the user wants to change his/her preferences after initializing them.

So, basic lifecycle:

  1. User adds the widget
  2. Configure activity pops up, user fills in fields and clicks “Submit”
  3. Widget is added on screen
  4. User taps the ImageView to launch the configure activity
  5. Configure activity pops up, user edits fields
  6. After either hitting “Submit” or backing out, the widget is updated
  7. User can continue to go through steps 5 and 6 as needed.

My problem is at step 5. The very first and only the first time that the user taps the ImageView, it looks like two configure activities are launched. That is, when I back out of the first one, there’s still another one “behind” it. On all subsequent launches of the configure activity, however, only one is launched and everything works great.

What could be the problem? I’ll post relevant code below.

AndroidManifest.xml

    <activity
        android:name=".ConfigActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".Widget"
        android:label="Widget" >
        <intent-filter>
            <action
                android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <intent-filter>
            <action
                android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <data android:scheme="sample_widget" />
        </intent-filter>
        <intent-filter>
            <action
                android:name="com.this.that.WIDGET_CONTROL" />
            <data
                android:scheme="sample_widget" />
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget" />
    </receiver>

AppWidget-Provider widget.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:updatePeriodMillis="5000"
    android:minWidth="294dp"
    android:minHeight="220dp"
    android:initialLayout="@layout/widgetlayout"
    android:configure="com.this.that.ConfigActivity" >
</appwidget-provider>

ConfigActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the data we were launched with
    Intent launchIntent = getIntent();
    Bundle extras = launchIntent.getExtras();
    if (extras != null) {
        appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

        Intent cancelResultValue = new Intent();
        cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        setResult(RESULT_CANCELED, cancelResultValue);
    } else {
        // Only launch if it's for configuration
        finish();
    }

    setContentView(R.layout.myconfig);

    // Create Buttons/EditTexts
    SubmitBTN = (Button) findViewById(R.id.BTNSubmit);
    SampleET= (EditText) findViewById(R.id.ETSample);
    SubmitBTN.setOnClickListener(submitListener);

    loadPreferences(ConfigActivity.this, appWidgetId);
}

private OnClickListener submitListener = new OnClickListener() {
    public void onClick(View v) {
        final Context context = PriorityViewConfig.this;

        // Save strings in our prefs
        String sample = SampleET.getText().toString();
        SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
        prefs.putString(PREF_PREFIX_KEY + appWidgetId + "sample", sample);
        prefs.commit();

        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            // Tell the AppWidgetManager that we're now configured
            Intent resultValue = new Intent();
            //resultValue.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            setResult(RESULT_OK, resultValue);

            // Get an instance of the AppWidgetManager
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            // Update the App Widget with the layout
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
            Widget.updateDisplayState(context, appWidgetId);
        }

        // Activity is now done
        finish();
    }
};

private void loadPreferences(Context context, int appWidgetId) {
    SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
    String sample = prefs.getString(PREF_PREFIX_KEY + appWidgetId + "sample", null);

    if (sample != null) {
        SampleET.setText(sample);
    } else {
        // Nothing stored, don't need to do anything
    }
}

Widget.java

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    Log.d(LOG_TAG, "OnReceive:Action: " + action);

    if (ACTION_WIDGET_CONTROL.equals(action)) {
        // Pass this on to the action handler where we'll figure out what to do and update the widget
        final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onHandleAction(context, appWidgetId, intent.getData());
        }           
    }
    super.onReceive(context, intent);
}

public static void updateDisplayState(Context context, int appWidgetId) {
    Intent configIntent = new Intent(context, ConfigActivity.class);
    configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    // Make this unique for this appWidgetId
    configIntent.setData(Uri.withAppendedPath(Uri.parse(Widget.URI_SCHEME + "://widget/id/"), String.valueOf(appWidgetId)));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.IVConfig, pendingIntent);

    AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);
}

private void onHandleAction(Context context, int appWidgetId, Uri data) {
    String controlType = data.getFragment();        
    // Nothing here yet
    updateDisplayState(context, appWidgetId);
}

I think these are the most relevant sections. The places I’m going to be looking further into are in ConfigActivity.java in the submitListener and in the updateDisplayState method in Widget.java

Any help would be awesome! Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:09:57+00:00Added an answer on May 23, 2026 at 1:09 pm

    When calling an explicit intent, you should consider using one of the intent flags like FLAG_ACTIVITY_CLEAR_TOP to keep the stack organized. For example:

    configIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    

    You can spot the lack of this flag in some apps like the IMDB app where each click of the Home button adds another instance of the home activity on the stack, so you need to click the Back button as many times to pop through the stack and back out of the app. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webview that loads a webpage. I also have a reload button.
I have an application that has a tab widget in it. One of the
I have an android widget which has a very simple function. The widget simply
I have an Activity that, for each widget in the layout, I call setOnClickListener()
I've just finished my Android widget. Now I need to have different sizes of
I have created an App Widget for Android 1.5. It uses a TextView to
I have android.permission.READ_OWNER_DATA but I can't find any reliable code that would explain how
I have an application, which has a Spinner that I want populated with some
Everyone, I am a newbie to android development. Now I have a question that
I have a ViewFlipper in my Activity that is used as a footer view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.