Android: why must use getBaseContext() instead of this

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

Dagger 2 injecting Android Application Context

@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

Call getLayoutInflater() in places not in activity

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

Get application context from non activity singleton class

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

get Context in non-Activity class [duplicate]

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

How can I share a SharedPreferences file across two different android apps?

It is better to set private mode for the file. App needs to be signed with same set of certificates to share this file. Set sharedUserId in both apps to be same. <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”com.example.hello” android:versionCode=”1″ android:versionName=”1.0″ android:sharedUserId=”com.example”> …. Get Context from other package: mContext = context.createPackageContext( “com.example.otherapp”, Context.MODE_PRIVATE); mPrefs = mContext.getSharedPreferences(“sameFileNameHere”, Activity.MODE_PRIVATE); Get items … Read more