Monday, 14 October 2013

get Context in non-Activity class (Android)

get Context in non-Activity class (Android)

To get the context in the non - activity class first 
we need the application class which extends Application
sample code:
public class Samp extends Application {

 private  static Context mContext;
 public static Context getContext() {
        return mContext;
    }

    public static void setContext(Context mContext1) {
        mContext = mContext1;
    }
   
}
In order to get the context in not Activity class create the instance in the non activity class as follows 
    private static Context context = samp.getContext();
samp.getContext(); // this will get the context for you any where ...



No comments:

Post a Comment