Skip to content

Commit 75e0af8

Browse files
committed
TaskStackBuilder correctness fixes
* Don't mutate original intents when adding default flags. * Add the relevant flags to the array returned by getIntents() such that it can be used directly in a call to startActivities or similar. * Deep copy the component intents when building an intent array for getIntents() * Clean up some internal code duplication Change-Id: I71d3b7f30d4f7d8f1cce778d406ea0e513d382c5
1 parent e2f0ec8 commit 75e0af8

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

core/java/android/app/TaskStackBuilder.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,7 @@ public void startActivities(Bundle options) {
216216
"No intents added to TaskStackBuilder; cannot startActivities");
217217
}
218218

219-
Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
220-
intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
221-
Intent.FLAG_ACTIVITY_CLEAR_TASK |
222-
Intent.FLAG_ACTIVITY_TASK_ON_HOME);
223-
mSourceContext.startActivities(intents, options);
219+
mSourceContext.startActivities(getIntents(), options);
224220
}
225221

226222
/**
@@ -260,11 +256,8 @@ public PendingIntent getPendingIntent(int requestCode, int flags, Bundle options
260256
"No intents added to TaskStackBuilder; cannot getPendingIntent");
261257
}
262258

263-
Intent[] intents = mIntents.toArray(new Intent[mIntents.size()]);
264-
intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
265-
Intent.FLAG_ACTIVITY_CLEAR_TASK |
266-
Intent.FLAG_ACTIVITY_TASK_ON_HOME);
267-
return PendingIntent.getActivities(mSourceContext, requestCode, intents, flags, options);
259+
return PendingIntent.getActivities(mSourceContext, requestCode, getIntents(),
260+
flags, options);
268261
}
269262

270263
/**
@@ -275,6 +268,15 @@ public PendingIntent getPendingIntent(int requestCode, int flags, Bundle options
275268
* @return An array containing the intents added to this builder.
276269
*/
277270
public Intent[] getIntents() {
278-
return mIntents.toArray(new Intent[mIntents.size()]);
271+
Intent[] intents = new Intent[mIntents.size()];
272+
if (intents.length == 0) return intents;
273+
274+
intents[0] = new Intent(mIntents.get(0)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
275+
Intent.FLAG_ACTIVITY_CLEAR_TASK |
276+
Intent.FLAG_ACTIVITY_TASK_ON_HOME);
277+
for (int i = 1; i < intents.length; i++) {
278+
intents[i] = new Intent(mIntents.get(i));
279+
}
280+
return intents;
279281
}
280282
}

0 commit comments

Comments
 (0)