What's the difference between the various methods to get a Context?

前端 未结 8 681
情书的邮戳
情书的邮戳 2020-11-22 04:06

In various bits of Android code I\'ve seen:

 public class MyActivity extends Activity {
    public void method() {
       mContext = this;    // since Activi         


        
8条回答
  •  余生分开走
    2020-11-22 05:07

    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.

提交回复
热议问题