0

So I am using getdefaultsharedpreferences in a method called onLoadFinish (it's from a pdf library from android). Here's the code:

    public void onLoadFinish(DocumentState.OPEN state) {
    //some irrelevant code here
    SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getActivity());
                String text = pref.getString("example_list","");
                int foo = Integer.parseInt(text);
                goToPage(foo);
    //some irrelevant code there
}

So the main task of the code is to get a value from my example_list preference (a string), turn it into an integer and put this integer into my goTopage();, which makes the app jump to a certain page in my pdf document.

The problem is this part:

PreferenceManager.getDefaultSharedPreferences(getActivity())

getActivity isn't working. I have tried getApplicationContext() aswell. What should be in the brackets of getDefaultSharedPreferences()?

just_deko
  • 1,024
  • 1
  • 14
  • 29

3 Answers3

0

PreferenceManager should be used with a PreferenceActivity.

Just use context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);

pdegand59
  • 12,776
  • 8
  • 51
  • 58
  • your first statement is wrong, it can be used anywhere, it just requires an instance of `Context` class. – nikis Mar 30 '15 at 09:49
0

getDefaultSharedPreferences required your application context a parameter, Try this,

    public class MyActivity extends ActionBarActivity
    {
    ......
    .......
    PreferenceManager.getDefaultSharedPreferences(MyActivity.this);
    ......
    ......

    }
Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67
0

getDefaultSharedPreferences expects an instance of Context class. getActivity method is declared in the Fragment class, so, unless your onLoadFinish method is declared in any Fragment successor, you can't use it. Per your comments, if I understood you correctly, onLoadFinish is declared inside Activity. If so, you can just use this keyword to pass the context, because Activity is a successor of Context. If this method is declared in another class, you should pass context to it, via constructor injection, for example.

EDIT Example of providing context via constructor injection.

Let's say you have the following interface:

public interface MyInterface {
    void myAction();
}

And you have a class, which implements it and requires an instance of Context to do the work:

public class MyClass implements MyInterface {

    private WeakReference<Context> mContext;

    public MyClass(Context context) {
        this.mContext = new WeakReference<Context>(context);
    }

    @Override
    public void myAction() {
        Context ctx = mContext.get();
        if (ctx != null){
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            //do stuff
        }
    }
}

As you can see, Context instance is injected via constructor and we don't keep a strong reference to the context (actually it depends on specific needs). This class can be used inside Activity in the following way:

MyClass myClass = new MyClass(this);

Or inside fragment:

MyClass myClass = new MyClass(getActivity());
nikis
  • 11,166
  • 2
  • 35
  • 45
  • Alright, when I'm using "this", android studio says, "this", cannot be applied to my "SimpleDocumentReader"class and wants me to make SimpleDocumentReader extend "Android.content.Context". Should I do this? – just_deko Mar 30 '15 at 10:07
  • @just_deko are you sure that `SimpleDocumentReader` is activity? can you show the signature of the class where you implemented `SimpleDocumentReaderListener` interface? – nikis Mar 30 '15 at 10:10
  • `public class SimpleDocumentReader implements ReaderListener {}` do you mean this? – just_deko Mar 30 '15 at 10:32
  • @just_deko yes. Looks like `SimpleDocumentReader` is not activity and just a plain class. So you should inject context via constructor, for example – nikis Mar 30 '15 at 10:35
  • Could you link me to a tutorial or something of that kind to give me an idea how to do this? Sorry, I am still a beginner in java programming and this is my first app. – just_deko Mar 30 '15 at 10:41
  • @just_deko I've added a simple example. This is the answer http://stackoverflow.com/questions/3572463/what-is-context-in-android where you can read more about the context – nikis Mar 30 '15 at 10:59
  • So, I've tried to apply this to my case. replaced MyClass with SimpleDocumentreader and MyAction with onLoadFinish and inserted the code there. No errors whatsoever but the the app isn't jumping to the page I want, meaning ctx is null. Have I forgot something? – just_deko Mar 30 '15 at 14:35
  • @just_deko i don't know how do you use `SimpleDocumentReader` inside your code. I've used `WeakReference` in my example, which means, that `Activity` may be garbage collected. You can try to use plain strong reference without wrapping into `WeakReference`, also please read about activity/fragment lifecycle – nikis Mar 30 '15 at 14:42
  • As this is a pdf library, I am using the code in my Activity (where I display the pdf document) like this: `SimpleDocumentReader v = SimpleReaderFactory.createSimpleViewer(this, null); v.openUrl("myurlhere", "123456");` (if that helps) – just_deko Mar 30 '15 at 14:44
  • @just_deko per https://plugpdf.com/devrefs/android/classcom_1_1epapyrus_1_1plugpdf_1_1_simple_reader_factory.html I can say, that you don't provide the listener (it's `null` per you code). Create class `MyClass implements SimpleDocumentReaderListener` and provide its instance as the second parameter – nikis Mar 30 '15 at 14:50
  • the second parameter for what? `createSimpleViewer`? – just_deko Mar 30 '15 at 15:49
  • @just_deko yes, docs clearly states, that second parameter is `listener The SimpleDocumentViewerListener instance.` – nikis Mar 30 '15 at 15:56