Reputation: 553
In my application getActivity()
returns null sometimes and so app crashes. This occurs very rarely, but it is reproducible when app is kept in background for long time. I use getActivity()
to get context at various places. Is there any other method to get context in application? Is there any standards suggested? Is it good to use application context everywhere?
Upvotes: 0
Views: 797
Reputation: 2495
As I understand you called getActivity() in your fragment. You should check method onDetach() after it getActivity will return null (your fragment is detached). If your code is related to current activity of fragment it will be better to just skip any work after detach. If your code is "all app life" its better to use ApplicationContext To use or not to user ApplicationContext is depends of your task and type of resouce.
Upvotes: 1