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

What is the difference between Activity and Context?

As far as I understand: Context is the Base Object. So every Activity same as Application derives from Context. This means that every Activity and every Application IS a Context; From developer.android.com Activity java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.view.ContextThemeWrapper ↳ android.app.Activity And Application java.lang.Object ↳ android.content.Context ↳ android.content.ContextWrapper ↳ android.app.Application An Application context lasts, … Read more

getting context in AsyncTask

You need to do following things. when you want to use AsyncTask, extend that in other class say MyCustomTask. in constructor of new class, pass Context Example public class MyCustomTask extends AsyncTask<Void, Void, Long> { private Context mContext; public MyCustomTask (Context context){ mContext = context; } //other methods like onPreExecute etc. protected void onPostExecute(Long result) … Read more

getApplicationContext(), getBaseContext(), getApplication(), getParent()

getApplicationContext() Application context is associated with the Applicaition and will always be the same throughout the life cycle. getBasecontext() should not be used, just use Context instead of it which is associated with the activity and could possible be destroyed when the activity is destroyed. getApplication() is available to Activity and Services only. Although in … Read more

What’s “tools:context” in Android layout files?

This is the activity the tools UI editor uses to render your layout preview. It is documented here: This attribute declares which activity this layout is associated with by default. This enables features in the editor or layout preview that require knowledge of the activity, such as what the layout theme should be in the … Read more

difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

Toast and Intent, both requires reference to context. And getApplication, getApplicationContext, LoginActivity.this and getBaseContext, they all offer reference to the context. Now the thing confuses is the declaration of different contexts and their specific-usage. To make things simple, you should count two types of context available in the Android framework. Application Context Activity Context Application … Read more