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

Will context.getApplicationContext return null happen? #356

Open
aiueoH opened this issue Jan 26, 2019 · 6 comments
Open

Will context.getApplicationContext return null happen? #356

aiueoH opened this issue Jan 26, 2019 · 6 comments

Comments

@aiueoH
Copy link

@aiueoH aiueoH commented Jan 26, 2019

According to this page Understanding the Android Application Class

The application object is not guaranteed to stay in memory forever

So the application will be released when somes components is alive? My app has some crash reports caused by application null pointer exception in service.

@kamiljeglikowski
Copy link

@kamiljeglikowski kamiljeglikowski commented Jan 26, 2019

Can you show those crashes?

@pablichjenkov
Copy link

@pablichjenkov pablichjenkov commented Jan 26, 2019

Possible in theory bizarre in practicality.

@nareshkatta99
Copy link

@nareshkatta99 nareshkatta99 commented Jan 27, 2019

Yes it will some times.
We faced the same issue.
You should use context.getApplicatinContext when it requires to place some thing global level.
You can use them for things like toast etc but not for creating UI etc.

@aiueoH
Copy link
Author

@aiueoH aiueoH commented Jan 28, 2019

Can you show those crashes?

It happened on a few devices. I can't reproduce.

@aeramli
Copy link

@aeramli aeramli commented Jan 28, 2019

Services are running in a different sandbox from the app, having a service running doesn't guarantee keeping the app alive.
You might have to change your approach of "setting things globally" -> Broadcast, sharedpreferences ...Etc

@aemxn
Copy link

@aemxn aemxn commented Jan 28, 2019

Careful when using getApplicationContext as it would cause memory leak when using it with long living object. Try some other contexts as described here

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

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.