Reputation: 3114
I have a notification receiver that is being called from my alarm.
The notification receiver bundles the data for a new intent (activity) and starts it like this:
// Create an in intent to go to the new alarm video
Intent repeatingAlarmIntent = new Intent(context,RepeatingAlarmActivity.class);
// Bundle the videoID the alarm has the correct video to open
Bundle alarmExtras = new Bundle();
extras.putString("AlarmVidId", VideoId);
repeatingAlarmIntent.putExtras(alarmExtras);
// Set the flags for the alarm intent
repeatingAlarmIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
repeatingAlarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Start the alarm intent
context.startActivity(repeatingAlarmIntent);
In the new activity when I try to retrieve the bundled data it is null, this is how I am attempting to retrieve the bundled data:
public class RepeatingAlarmActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
String VideoId;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
VideoId= intent.getStringExtra("AlarmVidId");
Not sure what I am doing wrong.
Thanks in advance for your help.
Upvotes: 0
Views: 184
Reputation: 4656
Replace all with the following:
Intent repeatingAlarmIntent = new Intent(context,RepeatingAlarmActivity.class);
repeatingAlarmIntent.putExtra("AlarmVidId", VideoId);
repeatingAlarmIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
repeatingAlarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(repeatingAlarmIntent);
Your problem is you created a Bundle
called alarmExtras
however you are putting the extra field in the variable called extra
.
There is no need to create Bundle
. just add the value directly to the Intent
.
Also, you are doing setFlags()
. If you need more than one, do addFlags()
instead
Upvotes: 1
Reputation: 3114
I worked out a solution.
I am pretty sure it is because of the lifecycle of the notification receiver and the context.
So instead of using the context that was passed when started the receiver I used that context to get the application context.
This method is working but I welcome comments on potential problems with this method as I have never had to solve something like this before.
This is what worked for me:
// Create an in intent to go to the new alarm video
Intent repeatingAlarmIntent = new Intent(context.getApplicationContext(),RepeatingAlarmActivity.class);
// Bundle the videoID the alarm has the correct video to open
Bundle alarmExtras = new Bundle();
extras.putString("AlarmVidId", VideoId);
repeatingAlarmIntent.putExtras(alarmExtras);
// Set the flags for the alarm intent
repeatingAlarmIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
repeatingAlarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Start the alarm intent
context.getApplicationContext().startActivity(repeatingAlarmIntent);
Above when creating the intent and starting the intent there is now:
getApplicationContext()
On the activity side were no changes required.
Upvotes: 0
Reputation: 4798
Your extras
variable when calling putString does not match the alarmExtras
are adding to the Intent.
Bundle alarmExtras = new Bundle();
alarmExtras.putString("AlarmVidId", VideoId);
repeatingAlarmIntent.putExtras(alarmExtras);
Upvotes: 0