0

I've been working on developing a library for my company for the past couple months and have been annoyed by the fact that filestreams seem to need a context whenever I store or load data to the internal storage.

I have designed the library to work like this :

  1. A singleton class is made for providing and handling a keychain(containing app key and device id) and authinfo(user and password)
  2. Whenever a request to call to a web service is made the calling class will attempt to get an instance of the singleton class and get the keychain and auth info through it like such :

    SingletonClass.getInstance().getCredentials(Context ctx);

The result of this is that I need to constantly provide the context of the calling activity as most of my library revolves around calling an API with credentials and device id as parameters.

I am specifically referring to these lines :

....
          FileOutputStream fos = ctx.openFileOutput(filename,                      
                   Context.MODE_PRIVATE);
          fos.write(buf);
          fos.close(); 
....

I am confused as to why the specific context of the activity calling is needed. Any help is greatly appreciated. Is there another way of solving this design issue ?

CodePrimate
  • 6,646
  • 13
  • 48
  • 86

2 Answers2

0

The file storage API is provided by the Context class. So you need it.

Vipul
  • 27,808
  • 7
  • 60
  • 75
  • That begs the question: If Activity X calls save with 'this'. Will Activity Y then be able to load this data from the internal storage? – CodePrimate Jun 05 '12 at 10:33
0

The application context should work for this - you could consider using this method: Using Application context everywhere? so you can easily get the application context without having to pass it around.

Community
  • 1
  • 1
Jords
  • 1,855
  • 3
  • 18
  • 28