I want to use it in my array adapter. When I put this
in a sub-activity to create an adapter it does not work.
getApplicationContext()
can get the context value
You can get context a few ways:
By the Activity, using Your_Activity_Name.this
By the application, using getApplicationContext()
By the View, using Your_View.getContext()
The only one I would not recommend is using getBaseContext()
. If you need something universal, have a public static variable in your main activity and assign the application context to it when your app starts. This way you can always call Your_Activity.your_context_variable
You can use getBaseContext()
but this is not a local context
.
Besides the correct previous answers, you may want to think about refactoring your code if you've come to the point where you need to access "your" context from a sub activity. When you create a sub activity (ie: startActivityForResult) you are truly waiting for a result, not for an action in the caller activity. Then, when the sub activity has finished (and you have the result of its calculations), you can access your context in a proper way. It just doesn't seem fine that the subactivity is aware of its creator, not to mention interact with it.
© 2022 - 2024 — McMap. All rights reserved.