jest034
jest034

Reputation: 1

How to open another view in a widget that stays on the homescreen?

I was using the Guardian news app and noticed that their widget had an options button that opened anther menu. I am not sure how this is done. If anyone has any ideas that would great.

Guardian Widget

Options menu open

Upvotes: 0

Views: 25

Answers (2)

Dima Kozhevin
Dima Kozhevin

Reputation: 3732

You can send an Intent as usual:

    public class YourWidget extends AppWidgetProvider {

    static PendingIntent getPendingSelfIntent(Context context, String action, int appWidgetId) {
        KLog.d(TAG, "getPendingSelfIntent appWidgetId = " + appWidgetId);
        Intent intent = new Intent(context, YourWidget.class);
        Bundle b = new Bundle();
        b.putInt("appWidgetId", appWidgetId);
        intent.putExtras(b);
        intent.setAction(action);
        PendingIntent pendInt = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendInt;
    }

    //....

    //your method for remote views 
    public void doUpdateView() {
         RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.collection_widget);
         remoteViews.setOnClickPendingIntent(R.id.button_settings, getPendingSelfIntent(context, RUN_CLICKED, appWidgetId));
   }

    @Override
    public void onReceive(final Context context, Intent intent) {
        super.onReceive(context, intent);
        if (RUN_CLICKED.equals(intent.getAction())) {
            Intent actIntent = new Intent(context, YourActivity.class);
            actIntent.putExtra("your_extra_key", your_extra_value);
            actIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(actIntent);
        } 
        //...
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
    }

Upvotes: 0

extmkv
extmkv

Reputation: 2045

This easily way it's open a dialog or a modal pop-up activity.

Upvotes: 1

Related Questions