Reputation: 4807
I am trying to pass data from widget to activity and I always receive null. Here is onUpdate from widget class:
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget);
Intent configIntent = new Intent(context, WebViewActivity.class);
configIntent.putExtra("URL", "Content");
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.buttonOpen, configPendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
Second Activity:
public class WebViewActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
String sessionId = getIntent().getStringExtra("URL");
Log.d("DTAG","URL: "+sessionId); //Is null
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(sessionId);
}
}
Upvotes: 2
Views: 242
Reputation: 1006664
If you wish to replace extras in a PendingIntent
, you need to use FLAG_UPDATE_CURRENT
for the final parameter to PendingIntent.getActivity()
. Otherwise, the extras in your new Intent
will be ignored.
And, if you wish to have different extras for different PendingIntent
objects (e.g., per widget ID), you need to pass a unique ID as the second parameter to PendingIntent.getActivity()
. If you use 0
all the time, you get exactly one PendingIntent
with one set of extras.
Upvotes: 3