Skip to content

Commit 32ef750

Browse files
adampAndroid (Google) Code Review
authored andcommitted
Merge "Add TaskStackBuilder#addParentStack(ComponentName)"
2 parents 27bfced + 3c464bd commit 32ef750

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

api/current.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3999,6 +3999,7 @@ package android.app {
39993999
method public android.app.TaskStackBuilder addNextIntent(android.content.Intent);
40004000
method public android.app.TaskStackBuilder addParentStack(android.app.Activity);
40014001
method public android.app.TaskStackBuilder addParentStack(java.lang.Class<?>);
4002+
method public android.app.TaskStackBuilder addParentStack(android.content.ComponentName);
40024003
method public static android.app.TaskStackBuilder from(android.content.Context);
40034004
method public android.content.Intent getIntent(int);
40044005
method public int getIntentCount();

core/java/android/app/TaskStackBuilder.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ public TaskStackBuilder addNextIntent(Intent nextIntent) {
9191

9292
/**
9393
* Add the activity parent chain as specified by the
94-
* {@link android.R.attr#parentActivityName parentActivityName} attribute of the activity
95-
* (or activity-alias) element in the application's manifest to the task stack builder.
94+
* {@link Activity#getParentActivityIntent() getParentActivityIntent()} method of the activity
95+
* specified and the {@link android.R.attr#parentActivityName parentActivityName} attributes
96+
* of each successive activity (or activity-alias) element in the application's manifest
97+
* to the task stack builder.
9698
*
9799
* @param sourceActivity All parents of this activity will be added
98100
* @return This TaskStackBuilder for method chaining
@@ -155,6 +157,41 @@ public TaskStackBuilder addParentStack(Class<?> sourceActivityClass) {
155157
return this;
156158
}
157159

160+
/**
161+
* Add the activity parent chain as specified by the
162+
* {@link android.R.attr#parentActivityName parentActivityName} attribute of the activity
163+
* (or activity-alias) element in the application's manifest to the task stack builder.
164+
*
165+
* @param sourceActivityName Must specify an Activity component. All parents of
166+
* this activity will be added
167+
* @return This TaskStackBuilder for method chaining
168+
*/
169+
public TaskStackBuilder addParentStack(ComponentName sourceActivityName) {
170+
final int insertAt = mIntents.size();
171+
PackageManager pm = mSourceContext.getPackageManager();
172+
try {
173+
ActivityInfo info = pm.getActivityInfo(sourceActivityName, 0);
174+
String parentActivity = info.parentActivityName;
175+
Intent parent = new Intent().setComponent(
176+
new ComponentName(info.packageName, parentActivity));
177+
while (parent != null) {
178+
mIntents.add(insertAt, parent);
179+
info = pm.getActivityInfo(parent.getComponent(), 0);
180+
parentActivity = info.parentActivityName;
181+
if (parentActivity != null) {
182+
parent = new Intent().setComponent(
183+
new ComponentName(info.packageName, parentActivity));
184+
} else {
185+
parent = null;
186+
}
187+
}
188+
} catch (NameNotFoundException e) {
189+
Log.e(TAG, "Bad ComponentName while traversing activity parent metadata");
190+
throw new IllegalArgumentException(e);
191+
}
192+
return this;
193+
}
194+
158195
/**
159196
* @return the number of intents added so far.
160197
*/

0 commit comments

Comments
 (0)