Reputation: 4830
I've got a widget in my application from which it should be possible to the application using startActivity()
and I need to also pass along some extras with the Intent
starting the activity. But in onCreate()
the Bundle
's are null!?!
Have tried to figure this out and someone has solved this by overriding onNewIntent()
as this could be called if an instance of my activity already exists, but no success in this either as onNewIntent()
never gets called. So what's happening here? I just want to start an activity from outside my application and pass some extras with the Intent
?
Intent intent= new Intent(context, SmsAlarm.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(SWITCH_FRAGMENT_REQUEST, true);
context.startActivity(intent);
As I told earlier, my activity starts fine this way but I loose my extras, which I need. Can anyone tell me whats and why this happens.
Upvotes: 0
Views: 305
Reputation:
Check this link. http://stackoverflow.com/questions/10299461/getting-extra-string-value-from-a-previous-activity-inside-an-onclick-method
To the other Activity, in onCreate method for take a string you must use
saveMe = getIntent().getExtras().getString("string");
If you have a boolean you can use
boolean defaultValue = false;
boolean yourValue = getIntent().getBooleanExtra(YOUR_EXTRA, defaultValue);
To Developer.android
Upvotes: 0
Reputation: 5746
The proper way to open an Activity intent from the widget is to use PendingIntent. You should not have any issues when doing that.
Setting up the PendingIntent as described in the docs:
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, ExampleActivity.class);
// Add Extra
intent.putExtra("MY_EXTRA", "extra");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
Now, get the extra as you normally would:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mMyExtra = extras.getString("MY_EXTRA", null);
}
MY_EXTRA should be a const somewhere.
Upvotes: 2