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 9274273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:18:59+00:00 2026-06-18T16:18:59+00:00

I have a configuration activity, a large widget provider and a small widget provider.

  • 0

I have a configuration activity, a large widget provider and a small widget provider. From configuration activity i save some values in shared preferences. From large and small app widget providers i get those shared preferences. I am not able to give the app widget unique ids and i want to have different shared preferences each time i move on from configuration activity to app widget provider. How can i achieve this.

  • 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-06-18T16:19:00+00:00Added an answer on June 18, 2026 at 4:19 pm

    To give unique widget ids for each instances of the app widget, you can do as follows:

        AppWidgetManager appWidgetManager = AppWidgetManager
                    .getInstance(ConfigurationActivity.this);
        ComponentName thisAppWidget = new ComponentName(
                    ConfigurationActivity.this, WidgetProvider.class);
        int[] appWidgetIds = appWidgetManager
                .getAppWidgetIds(thisAppWidget);
        Intent startBroadcast = new Intent(ConfigurationActivity.this,
                    WidgetProvider.class);
        startBroadcast.putParcelableArrayListExtra("list", newsList);
        startBroadcast.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
                    appWidgetIds);
        startBroadcast.setAction("android.appwidget.action.APPWIDGET_UPDATE");
            sendBroadcast(startService);
    

    In order to save data in unique shared preference, you can do as follows:

        public void setWidgetNewsCategory(Context context, String category,
            int appWidgetId) {
    
        Editor editor = context.getSharedPreferences(
                PREFS_NAME_CATEGORY + String.valueOf(appWidgetId),
                Context.MODE_PRIVATE).edit();
        editor.putString(PREFS_VALUE_CATEGORY + String.valueOf(appWidgetId),
                category);
        editor.commit();
    }
    

    You can retrieve this shared pref value as follows:

         public String getWidgetNewsCategory(Context context, int appWidgetId) {
         SharedPreferences sharedPreferences = context.getSharedPreferences(
                PREFS_NAME_CATEGORY + String.valueOf(appWidgetId),
                Context.MODE_PRIVATE);
        return sharedPreferences.getString(
                PREFS_VALUE_CATEGORY + String.valueOf(appWidgetId), null);
    }
    

    And finally,in Widget Provider’s onReceive method, do as follows:

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                int[] appWidgetIds = extras
                        .getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);             
                if (appWidgetIds.length > 0) {
                    this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);//here you can call onUpdate method, and update your views as you wish
                }
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null
                    && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
                final int appWidgetId = extras
                        .getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
                this.onDeleted(context, new int[] { appWidgetId });
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
            this.onEnabled(context);
        } else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
            this.onDisabled(context);
        }
    }
    

    This is just general solution which has worked for me. Hopefully it will work for you as well.

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

Sidebar

Related Questions

I have an app widget with a configuration activity. When I click done in
I have a Widget with his android.appwidget.action.APPWIDGET_CONFIGURE Activity with some preferences witch I can
I have configuration values saved in an app.config . I want to create a
I am trying display app widget( with configuration activity. where user can choose the
I have one configuration activity and multiple app widgets for the same app. When
colleagues! My problem is: I`ve got an App Widget with Configuration Activity(min SDK -
I have an app with a main activity and an app widget. The app
i have one activity and in onCreate i load language from preferences and set
I have a three-page/Activity setup/configuration piece to my app. I want to have two
I am developing an android app (only Widget) which displays some images from a

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.