Getting the Application Context

后端 未结 6 1200
梦如初夏
梦如初夏 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: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;

提交回复
热议问题