1515 */
1616package org .androidannotations .helper ;
1717
18- import static com .sun .codemodel .JExpr ._new ;
19- import static com .sun .codemodel .JExpr ._this ;
20- import static com .sun .codemodel .JExpr .cast ;
21- import static com .sun .codemodel .JMod .FINAL ;
22- import static com .sun .codemodel .JMod .PRIVATE ;
23- import static com .sun .codemodel .JMod .PUBLIC ;
24- import static com .sun .codemodel .JMod .STATIC ;
18+ import static com .sun .codemodel .JExpr .*;
19+ import static com .sun .codemodel .JMod .*;
2520import static javax .lang .model .element .ElementKind .CONSTRUCTOR ;
2621
2722import java .lang .reflect .Field ;
@@ -333,6 +328,9 @@ private void addIntentBuilder(JCodeModel codeModel, EBeanHolder holder, boolean
333328 constructorBody .assign (contextField , constructorContextParam );
334329 constructorBody .assign (holder .intentField , _new (intentClass ).arg (constructorContextParam ).arg (holder .generatedClass .dotclass ()));
335330 }
331+ // Additional constructor for fragments (issue #541)
332+ JFieldVar fragmentField = addIntentBuilderFragmentConstructor (holder , holder .classes ().FRAGMENT , "fragment_" , contextField );
333+ JFieldVar fragmentSupportField = addIntentBuilderFragmentConstructor (holder , holder .classes ().SUPPORT_V4_FRAGMENT , "fragmentSupport_" , contextField );
336334
337335 {
338336 // get()
@@ -360,7 +358,27 @@ private void addIntentBuilder(JCodeModel codeModel, EBeanHolder holder, boolean
360358
361359 JBlock body = method .body ();
362360 JClass activityClass = holder .classes ().ACTIVITY ;
363- JConditional condition = body ._if (contextField ._instanceof (activityClass ));
361+
362+ JConditional condition = null ;
363+ if (fragmentSupportField != null ) {
364+ condition = body ._if (fragmentSupportField .ne (JExpr ._null ()));
365+ condition ._then () //
366+ .invoke (fragmentSupportField , "startActivityForResult" ).arg (holder .intentField ).arg (requestCode );
367+ }
368+ if (fragmentField != null ) {
369+ if (condition == null ) {
370+ condition = body ._if (fragmentField .ne (JExpr ._null ()));
371+ } else {
372+ condition = condition ._elseif (fragmentField .ne (JExpr ._null ()));
373+ }
374+ condition ._then () //
375+ .invoke (fragmentField , "startActivityForResult" ).arg (holder .intentField ).arg (requestCode );
376+ }
377+ if (condition == null ) {
378+ condition = body ._if (contextField ._instanceof (activityClass ));
379+ } else {
380+ condition = condition ._elseif (contextField ._instanceof (activityClass ));
381+ }
364382 condition ._then () //
365383 .invoke (JExpr .cast (activityClass , contextField ), "startActivityForResult" ).arg (holder .intentField ).arg (requestCode );
366384 condition ._else () //
@@ -383,4 +401,22 @@ private void addIntentBuilder(JCodeModel codeModel, EBeanHolder holder, boolean
383401 }
384402 }
385403 }
404+
405+ private JFieldVar addIntentBuilderFragmentConstructor (EBeanHolder holder , JClass fragmentClass , String fieldName , JFieldVar contextField ) {
406+
407+ try {
408+ Class .forName (fragmentClass .fullName (), false , getClass ().getClassLoader ());
409+ } catch (ClassNotFoundException e ) {
410+ // do nothing if the class is not in the classpath
411+ return null ;
412+ }
413+ JFieldVar fragmentField = holder .intentBuilderClass .field (PRIVATE , fragmentClass , fieldName );
414+ JMethod constructor = holder .intentBuilderClass .constructor (JMod .PUBLIC );
415+ JVar constructorFragmentParam = constructor .param (fragmentClass , "fragment" );
416+ JBlock constructorBody = constructor .body ();
417+ constructorBody .assign (fragmentField , constructorFragmentParam );
418+ constructorBody .assign (contextField , constructorFragmentParam .invoke ("getActivity" ));
419+ constructorBody .assign (holder .intentField , _new (holder .classes ().INTENT ).arg (contextField ).arg (holder .generatedClass .dotclass ()));
420+ return fragmentField ;
421+ }
386422}
0 commit comments