Getting the Application Context

后端 未结 6 1197
梦如初夏
梦如初夏 2020-12-02 23:16

This might be a simple question but I just wanted to make sure I am right.

In my android application I have a constructor that uses:

activity.getApp         


        
相关标签:
6条回答
  • 2020-12-02 23:48

    There is only one application context, so you should get the same one. You can have just one constructor that takes a Context, you don't really need two. Or if you wanted to make sure that you are getting the application context, and not, say, an activity one, you can have your constructor take Application as a parameter which is a Context.

    0 讨论(0)
  • 2020-12-02 23:50

    You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.

    0 讨论(0)
  • 2020-12-02 23:51

    Application Context add Activity Context both are different.Downcasting is risky .Use this code to use context object .

    public class App extends Application {
    public static Context context;
    
        @Override public void onCreate() {
            super.onCreate();
            context = getApplicationContext();
        }
    } 
    

    In Your Activities and in fragments Class :

    Conetext context=App.context;

    0 讨论(0)
  • 2020-12-02 23:58

    Will I get the same application context?

    Yes. You can check the android documentation, they have provided

     getApplicationContext()
    

    Return the context of the single, global Application object of the current process.

    So it should not be changed for the whole application process.

    Please also take a note of this:

    getApplicationContext() generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.

    Correct me if I'm wrong.

    Thanks

    0 讨论(0)
  • 2020-12-03 00:07

    The easiest way to get the application context is:

    Create a class App that extends android.app.Application

    public class App extends Application {
        public static Context context;
    
        @Override public void onCreate() {
            super.onCreate();
            context = getApplicationContext();
        }
    }
    

    Modify your AndroidManifest.xml 's <application> tag to have the attribute android:name="your.package.name.App".

    Any time you need the application context, just get it from App.context.

    Application is always initialized first whether your process runs, whether it's an activity, a service, or something else. You will always have access to the application context.

    0 讨论(0)
  • 2020-12-03 00:10

    I have adapted yuku's answer with a non static direct context reference.

    Create a class domain.company.pseudo.ApplicationName which extends android.app.Application.

    package hypersoft.systems.android;
    
    import android.app.Application;
    
    public class Starbox extends Application {
    
      public static Starbox instance;
    
      @Override
      public void onCreate() {
        super.onCreate();
        instance = this;
      }
    
    }
    

    In this sample, my full application package name is hypersoft.systems.android.starbox.

    Now, modify your AndroidManifest.xml <application> tag to have the attribute android:name="hypersoft.systems.android.Starbox", and be sure the Starbox.java class file is located in the project component directory: android rather than starbox.

    With all this done, you can now import hypersoft.systems.android.Starbox, and in your code you can get the ApplicationContext by calling Starbox.instance.getApplicationContext()

    Successfully compiling with build tools 26 and api 26 (Android 8.0) with min sdk version 14 (4.0).

    0 讨论(0)
提交回复
热议问题