@@ -124,24 +124,12 @@ public TaskStackBuilder addNextIntentWithParentStack(Intent nextIntent) {
124124 * @return This TaskStackBuilder for method chaining
125125 */
126126 public TaskStackBuilder addParentStack (Activity sourceActivity ) {
127- final int insertAt = mIntents .size ();
128- Intent parent = sourceActivity .getParentActivityIntent ();
129- PackageManager pm = sourceActivity .getPackageManager ();
130- while (parent != null ) {
131- mIntents .add (insertAt , parent );
132- try {
133- ActivityInfo info = pm .getActivityInfo (parent .getComponent (), 0 );
134- String parentActivity = info .parentActivityName ;
135- if (parentActivity != null ) {
136- parent = new Intent ().setComponent (
137- new ComponentName (mSourceContext , parentActivity ));
138- } else {
139- parent = null ;
140- }
141- } catch (NameNotFoundException e ) {
142- Log .e (TAG , "Bad ComponentName while traversing activity parent metadata" );
143- throw new IllegalArgumentException (e );
144- }
127+ final Intent parent = sourceActivity .getParentActivityIntent ();
128+ if (parent != null ) {
129+ // We have the actual parent intent, build the rest from static metadata
130+ // then add the direct parent intent to the end.
131+ addParentStack (parent .getComponent ());
132+ addNextIntent (parent );
145133 }
146134 return this ;
147135 }
@@ -155,24 +143,7 @@ public TaskStackBuilder addParentStack(Activity sourceActivity) {
155143 * @return This TaskStackBuilder for method chaining
156144 */
157145 public TaskStackBuilder addParentStack (Class <?> sourceActivityClass ) {
158- final int insertAt = mIntents .size ();
159- PackageManager pm = mSourceContext .getPackageManager ();
160- try {
161- ActivityInfo info = pm .getActivityInfo (
162- new ComponentName (mSourceContext , sourceActivityClass ), 0 );
163- String parentActivity = info .parentActivityName ;
164- while (parentActivity != null ) {
165- Intent parent = new Intent ().setComponent (
166- new ComponentName (mSourceContext , parentActivity ));
167- mIntents .add (insertAt , parent );
168- info = pm .getActivityInfo (parent .getComponent (), 0 );
169- parentActivity = info .parentActivityName ;
170- }
171- } catch (NameNotFoundException e ) {
172- Log .e (TAG , "Bad ComponentName while traversing activity parent metadata" );
173- throw new IllegalArgumentException (e );
174- }
175- return this ;
146+ return addParentStack (new ComponentName (mSourceContext , sourceActivityClass ));
176147 }
177148
178149 /**
@@ -191,11 +162,13 @@ public TaskStackBuilder addParentStack(ComponentName sourceActivityName) {
191162 ActivityInfo info = pm .getActivityInfo (sourceActivityName , 0 );
192163 String parentActivity = info .parentActivityName ;
193164 while (parentActivity != null ) {
194- Intent parent = new Intent ().setComponent (
195- new ComponentName (info .packageName , parentActivity ));
196- mIntents .add (insertAt , parent );
197- info = pm .getActivityInfo (parent .getComponent (), 0 );
165+ final ComponentName target = new ComponentName (mSourceContext , parentActivity );
166+ info = pm .getActivityInfo (target , 0 );
198167 parentActivity = info .parentActivityName ;
168+ final Intent parent = parentActivity == null && insertAt == 0
169+ ? Intent .makeMainActivity (target )
170+ : new Intent ().setComponent (target );
171+ mIntents .add (insertAt , parent );
199172 }
200173 } catch (NameNotFoundException e ) {
201174 Log .e (TAG , "Bad ComponentName while traversing activity parent metadata" );
0 commit comments