How to get activity context in android without activity?

I have an Activity class from which I pass some information to a Non-activity class. In the helper class, I want to use getSharedPreferences() . But I cannot use it because it requires an activity context.

here is my code:

  class myActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.home); Info = new Authenticate().execute(ContentString).get(); ItemsStore.SetItems(Info); } } class ItemsStore { public void SetItems(Information info) { SharedPreferences localSettings = mContext.getSharedPreferences("FileName", Context.MODE_PRIVATE); SharedPreferences.Editor editor = localSettings.edit(); editor.putString("Url", info.Url); editor.putString("Email", info.Email); } } 

ANY idea on how this can be achieved?

+6
source share
4 answers

Try the following:

 class myActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.home); Info = new Authenticate().execute(ContentString).get(); ItemsStore.SetItems(Info, getApplicationContext()); } } class ItemsStore { public void SetItems(Information info, Context mContext) { SharedPreferences localSettings = mContext.getSharedPreferences("FileName", Context.MODE_PRIVATE); SharedPreferences.Editor editor = localSettings.edit(); editor.putString("Url", info.Url); editor.putString("Email", info.Email); } } 
+7
source

Instead of creating memory leaks (by holding the activity context in the class field), you can try this solution because general settings do not need an activity context, but ... any context :). For long live objects you should use ApplicationContext.

Create an application class:

 public class MySuperAppApplication extends Application { private static Application instance; @Override public void onCreate() { super.onCreate(); instance = this; } public static Context getContext() { return instance.getApplicationContext(); } } 

Register it in the manifest

 <application ... android:name=".MySuperAppApplication" > ... </application> 

Then you can do something like this

 public void persistItems(Information info) { Context context = MySuperAppApplication.getContext(); SharedPreferences sharedPreferences = context.getSharedPreferences("urlPersistencePreferences", Context.MODE_PRIVATE); sharedPreferences.edit() .putString("Url", info.Url) .putString("Email", info.Email); } 

The method signature looks better because it does not need an external context. This can be hidden under some interface. You can also easily use it for dependency injection.

NTN

+12
source

You need to pass context to the inactivity class constructor

 ItemsStore itemstore = new ItemStore(myActivity.this); itemstore.SetItems(Info); 

Then

 Context mContext; public ItemsStore (Context context) { mContext =context; } 

Now mContext can be used as an activity context.

Note. Do not keep long-term references to context activity (a reference to an activity must have the same life cycle as the activity itself).

+4
source

Write a public function in your activity. When you instantiate your helper class in the Activity class, pass the activity context in the constructor.

Then, from your helper class, using the activity context, call the public function in the activity class.

0
source

All Articles