Android Get Application’s ‘Home’ Data Directory
Of course, never fails. Found the solution about a minute after posting the above question… solution for those that may have had the same issue: ContextWrapper.getFilesDir() Found here.
Of course, never fails. Found the solution about a minute after posting the above question… solution for those that may have had the same issue: ContextWrapper.getFilesDir() Found here.
Update: 06-Mar-18 Use MyApplication instance instead of Context instance. Application instance is a singleton context instance itself. public class MyApplication extends Application { private static MyApplication mContext; @Override public void onCreate() { super.onCreate(); mContext = this; } public static MyApplication getContext() { return mContext; } } Previous Answer You can get the the application context … Read more
@Module public class MainActivityModule { private final Context context; public MainActivityModule (Context context) { this.context = context; } @Provides //scope is not necessary for parameters stored within the module public Context context() { return context; } } @Component(modules={MainActivityModule.class}) @Singleton public interface MainActivityComponent { Context context(); void inject(MainActivity mainActivity); } And then MainActivityComponent mainActivityComponent = DaggerMainActivityComponent.builder() … Read more
You can use an Application context which is provided by the AndroidViewModel, you should extend AndroidViewModel which is simply a ViewModel that includes an Application reference.
getApplicationContext () returns the application context of the entire application life cycle,when application will destroy then it will destroy also. this the context returns the current context of the activity, belong to the activity, the activity is destroyed then it will destroy also.but in your case it will refers to the Spinner instance because we … Read more
You can use this outside activities – all you need is to provide a Context: LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); Then to retrieve your different widgets, you inflate a layout: View view = inflater.inflate( R.layout.myNewInflatedLayout, null ); Button myButton = (Button) view.findViewById( R.id.myButton ); EDIT as of July 2014 Davide’s answer on how … Read more
You can reference an outer context when you define your DialogInterface.OnClickListener as an anonymous class. If you’re in an activity you can use MyActivity.this as the context. Edit – since your Activity is implementing DialogInterface.OnClickListener, you should be able to just use this as the context.
Just to get a feeling of what’s going on. Activity extends ContextThemeWrapper which extends ContextWrapper from whom Activity inherits getApplicationContext(). ContextWrapper implements it as : @Override public Context getApplicationContext() { return mBase.getApplicationContext(); // mBase is a Context } The only public constructor of ContextWrapper is : public ContextWrapper(Context base) { mBase = base; } in … Read more
If your class is non-activity class, and creating an instance of it from the activiy, you can pass an instance of context via constructor of the later as follows: class YourNonActivityClass{ // variable to hold context private Context context; //save the context recievied via constructor in a local variable public YourNonActivityClass(Context context){ this.context=context; } } … Read more
An Activity inherits a Context. AlertDialog.Builder specifies a Context argument because it can then be used by ANY class that is a subclass of Context, including an Activity, ListActivity, Service, … (There is a common coding idiom behind this – you can learn more about it by reading Item I8 (on Interfaces and Abstract classes) … Read more