Android - what’s the difference between the various methods to get a Context?
2010-08-27 20:21
483 查看
Q:
In various bits of Android code I've seen:
[/code]
However I can't find any decent explanation of which is preferable, and under
what circumstances which should be used.
Pointers to documentation on this, and guidance about what might break if the
wrong one is chosen, would be much appreciated.
A:
5
I agree that documentation is sparse when it comes to Contexts in Android,
but you can piece together a few facts from various sources.
This blog post
on the official Google Android
developers blog was written mostly to help address memory leaks, but provides
some good information about contexts as well:
In a regular Android application, you usually have two kinds of Context,
Activity and Application.
Reading the article a little bit further tells about the difference between
to the two and why you might want to consider using the application Context
(Activity.getApplicaitonContext()) rather than using the Activity context
("this"). Basically the Application context is associated with the Applicaiton
and will always be the same throughout the life cycle of you app, where as the
Activity context is associated with the activity and could possible be destroyed
many times as the activity is destroyed during screen orientation changes and
such.
I couldn't find really anything about when to use getBaseContext() other than
a for a one liner from Dianne Hackborn, one of the Google engineers working on
the Android SDK:
Don't use getBaseContext(), just use the Context you have.
That was from a post on the android-developers newsgroup
, you may want to
consider asking your question there as well because a handful of the people
working on Android actual monitor that newsgroup and answer questions.
So overall it seems preferable to use the global application context when
possible.
----------------------------------------
1
First, I agree that we should use appcontext whenever possible. then "this"
in activity. i've never had a need for basecontext.
In my tests, in most cases they can be interchanged. In most cases, the
reason you want to get a hold of a context is to access files, preferences,
database etc. These data is eventually reflected as files in your app's private
data folder (/data/data/). No matter which context you use, they'll be mapped to
the same folder/files so you are OK.
That's what I observed. Maybe there are cases you should distinguish
them.
In various bits of Android code I've seen:
public class MyActivity extends Activity { public void method () { mContext = this ; // since Activity extends Context mContext = getApplicationContext (); mContext = getBaseContext (); } }
[/code]
However I can't find any decent explanation of which is preferable, and under
what circumstances which should be used.
Pointers to documentation on this, and guidance about what might break if the
wrong one is chosen, would be much appreciated.
A:
5
I agree that documentation is sparse when it comes to Contexts in Android,
but you can piece together a few facts from various sources.
This blog post
on the official Google Android
developers blog was written mostly to help address memory leaks, but provides
some good information about contexts as well:
In a regular Android application, you usually have two kinds of Context,
Activity and Application.
Reading the article a little bit further tells about the difference between
to the two and why you might want to consider using the application Context
(Activity.getApplicaitonContext()) rather than using the Activity context
("this"). Basically the Application context is associated with the Applicaiton
and will always be the same throughout the life cycle of you app, where as the
Activity context is associated with the activity and could possible be destroyed
many times as the activity is destroyed during screen orientation changes and
such.
I couldn't find really anything about when to use getBaseContext() other than
a for a one liner from Dianne Hackborn, one of the Google engineers working on
the Android SDK:
Don't use getBaseContext(), just use the Context you have.
That was from a post on the android-developers newsgroup
, you may want to
consider asking your question there as well because a handful of the people
working on Android actual monitor that newsgroup and answer questions.
So overall it seems preferable to use the global application context when
possible.
----------------------------------------
1
First, I agree that we should use appcontext whenever possible. then "this"
in activity. i've never had a need for basecontext.
In my tests, in most cases they can be interchanged. In most cases, the
reason you want to get a hold of a context is to access files, preferences,
database etc. These data is eventually reflected as files in your app's private
data folder (/data/data/). No matter which context you use, they'll be mapped to
the same folder/files so you are OK.
That's what I observed. Maybe there are cases you should distinguish
them.
相关文章推荐
- What is the Difference Between `apt-get purge` and `apt-get remove`?
- Whats the difference between Thread.setPriority() and android.os.Process.setThreadPriority()
- What is the difference between the ways to implement inheritance in javascript.
- What is the difference between SET and SELECT when assigning values to variables, in T-SQL?
- What’s the difference between alignment, de novo assembly, and map to reference
- What is the difference between dpkg and aptitude/apt-get?
- What is the difference between the Linux Kernel and the Android Kernel?
- What are some of the differences between using recursion to solve a problem versus using iteration?
- What is the difference between << and += while injecting elements to Hash.new([])?
- What is the difference between LINQ to SQL and LINQ to Entities?
- What is the difference between concurrency, parallelism and asynchronous methods?
- What is the difference between ./ and sh to run a script?
- Methods GET and POST in HTML forms - what's the difference?
- android: what is the difference between focused, enabled, pressed, and selected states?
- What is the difference between getWidth/heigth() and getMeasuredWidth/Heigth() in Android SDK?
- What is the difference between SVD and matrix factorization in context of recommendation engine?
- SharePoint : The difference and relation between SPListItem.Update and SPListItem.SystemUpdate Methods 【转载】
- What is the difference between BIT and TINYINT in MySQL
- 进程与线程的区别 What is the difference between process and thread
- What’s the difference between a stack and a heap?