Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ApplicationContext doesn't work with fragments. #2

Open
s0nerik opened this issue Jan 15, 2015 · 7 comments
Open

@ApplicationContext doesn't work with fragments. #2

s0nerik opened this issue Jan 15, 2015 · 7 comments
Labels

Comments

@s0nerik
Copy link

s0nerik commented Jan 15, 2015

Service field annotated with @ApplicationContext is null when injecting service into fragment.

Here's code from fragment:

    @InjectService
    PdfGeneratorService pdfGeneratorService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AsyncService.inject(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AsyncService.unregister(this);
    }

PdfGeneratorService:

@AsyncService
public class PdfGeneratorService {

    @ApplicationContext
    static Context context;

    public File generateClassRecordsTable(List<Student> students) {
        return new ClassTableCreator(context, students, context.getFilesDir()).create();
    }

}

Further investigation lead me to AsyncService.java, and here's the code that supposed to get Application context from object.

    /**
     * If the given object contains an Android context, extract
     * the application context and retain it statically.
     */
    private static void extractContextFromObject(Object object) {
        if (context == null && object instanceof Context) {
            context = ((Context) object).getApplicationContext();
        }
    }

But fragments doesn't have context, so I need to have a way to pass some context while injecting into fragment. Maybe some kind of builder approach for this would be better for this?

@JoanZapata
Copy link
Owner

As a workaround, have you tried using AsyncService.inject(this) in your Application's onCreate method? It should save the context from there.

@s0nerik
Copy link
Author

s0nerik commented Jan 20, 2015

Thanks, it works. But this way of initialization wasn't clear for me when I ran into this issue. Maybe it would be better to make some initialization method which will take Context or (maybe even better) Application?

@JoanZapata
Copy link
Owner

Yep, exactly. I thought on the moment that it would work for most cases and then I forgot about it.
I'll make one. Thanks for reporting.

@s0nerik
Copy link
Author

s0nerik commented Jan 20, 2015

Thank you for this lib, it's great!

@htekgulds
Copy link

Can you please give an example for that workaround?

@JoanZapata
Copy link
Owner

public class YourApplication extends Application {
    @Override
    public void onCreate() {
         AsyncService.inject(this);
    }
}

Don't forget to declare this application class in your AndroidManifest.xml.

@htekgulds
Copy link

Thanks a lot! Your work is appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants