'How to update multiple types appwidgets from the fragment of ConfigActivity by pressing one button?

I've made 2 different types of appwidgets showing some calendar data.

Each type has it's own layout, AppWidgetProvider class, AppWidgetProviderInfo object, but both of them use the same Configuration Activity based on one layout.

  • 1th type: WidgetTodayProvider.
  • 2th type: WidgetPeriodProvider

Inside Configuration Activity there is a Fragment having button "START". This button has to start selected appwidget for the first time + update the appwidget, because (I know) this is a Configuration activity's responsibility to update the widget for the first time.

At the moment I have the code for the button START, which can update only one type of the widget (WidgetTodayProvider):

buttonStart.setOnClickListener {
        val intent = Intent(
            AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, context,
            WidgetTodayProvider::class.java
        )
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf((activity as ConfigActivity).appWidgetId))
        activity?.sendBroadcast(intent)

        activity?.setResult(RESULT_OK, (activity as ConfigActivity).resultValue)
        activity?.finish()
    }

But I don't know how to write the code for the button START which would update both types of appwidgets (WidgetTodayProvider or WidgetPeriodProvider) depending on which of them is selected by the user...

How to define selected widget type inside the code of the button START and update this type of widget depending on the user's choice?



Solution 1:[1]

I've solved the problem. Inside the fragment of ConfigActivity:

val appWidgetManager = AppWidgetManager.getInstance(context)
    // We have a single ConfigActivity for all the types of the widgets,
    // thus we have to differ widget types.
    // Get string with short name of widget provider (looks like ".WidgetTodayProvider"),
    // using getAppWidgetInfo.provider.shortClassName.
    // Delete dot from the string, using substring(1).
    val widgetProviderShortName =
        appWidgetManager.
        getAppWidgetInfo((activity as ConfigActivity).appWidgetId)
            .provider.shortClassName
            .substring(1)

And the code for the START button depending on the name of AppWidgetProvider:

// Button Start (Start Widget)
    buttonStart.setOnClickListener {
        // Update appwidget from ConfigActivity
        when(widgetProviderShortName) {
            "WidgetTodayProvider" ->
                intent = Intent(
                    AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, context,
                    WidgetTodayProvider::class.java
                )
            "WidgetPeriodProvider" ->
                intent = Intent(
                    AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, context,
                    WidgetPeriodProvider::class.java
                )
        }
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, (activity as ConfigActivity).appWidgetId)
        activity?.sendBroadcast(intent)

        activity?.setResult(RESULT_OK, (activity as ConfigActivity).resultValue)
        activity?.finish()
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bic55