Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get activity context into a non-activity class android?

I have a Activity class from where I am passing some information to a helper class(Non-activity) class. In the helper class I want to use the getSharedPreferences(). But I am unable to use it as it requires the 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 how this can be achieved?

like image 997
user3354605 Avatar asked Mar 13 '14 06:03

user3354605


People also ask

How do you find the activity context in an application class?

From B activity you create a object of class A using this constructor and passing getApplicationContext(). Show activity on this post. If you need the context of A in B, you need to pass it to B, and you can do that by passing the Activity A as parameter as others suggested.

How do you find the context of a current activity?

onCreate() , register the callback. Now you can access the current foreground activity name and context. Show activity on this post. Show activity on this post.


2 Answers

Instead of creating memory leaks (by holding activity context in a class field) you can try this solution because shared preferences do not need activity context but ... any context :) For long living objects you should use ApplicationContext.

Create the 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 at 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);
}

Method signature looks better this way because it does not need external context. This can be hide under some interface. You can also use it easily for dependency injection.

HTH

like image 89
Gaskoin Avatar answered Sep 18 '22 12:09

Gaskoin


Try this:

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);
   }
}
like image 33
Mohammad Ashfaq Avatar answered Sep 20 '22 12:09

Mohammad Ashfaq