Question

I am hoping someone can give me some direction on how to access SharedPreferences from within a button's onclick event defined within the onViewCreated event.

I can set the values of my interface within the onViewCreated event by calling:

SharedPreferences prefs = this.getActivity().getSharedPreferences(
    "com.example.app", Context.MODE_PRIVATE);

and obtaining the appropriate values, but I have a button on the interface to be pressed to save changes and I can't seem to come up with the proper way to access the SharedPreferences from within my button's click event:

Button btn = (Button)view.findViewById(R.id.btnSave);
btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
        Log.d("test","here");
        SharedPreferences prefs = this.getActivity().getSharedPreferences(
                "com.example.app", Context.MODE_PRIVATE);

    }
});

This code obviously doesn't work since here this is referring to the view and getActivity doesn't work from the view. Can anyone tell me how to access the Activity from here so I can then access the SharedPreferences?

Was it helpful?

Solution

Hi there and welcome to StackOverflow.

The reason why you can't access SharedPreferences is because this is NOT the fragment: If you look closer, you'll realize that you have 2 nested contexts, so this is really an OnClickListener object (that is inside your fragment).

When you need to access a "parent context", you do so like this:

 public class MyCoolFragment extends Fragment {

    // here "this" is in fact your Fragment,
    // so this.getActivity().getSharedPreferences() DOES exist

    .
    .
    .

    Button btn = (Button)findViewById(R.id.btnSave);
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            // Perform action on click
            // "this" is NO LONGER your fragment, but the View.OnClickListener
            // to access the PARENT CONTEXT you prepend the CLASS NAME:

            // MyCoolFragment.this.getActivity().getSharedPreferences will work:


            SharedPreferences prefs = MyCoolFragment.this.getActivity().getSharedPreferences()
                    "com.example.app", Context.MODE_PRIVATE);

        }
    });

It is very typical in Java to have deeply-nested contexts, so you have to tell Java what this you want.

Also, remember you can easily get the context from ANY VIEW., so inside the click handler you could also do:

        Button btn = (Button)view.findViewById(R.id.btnSave);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                Activity myActivity=(Activity)(v.getContext()); // all views have a reference to their context
                SharedPreferences prefs =myActivity.getSharedPreferences(
                        "com.example.app", Context.MODE_PRIVATE);

            }
        });

Hope it helps !

OTHER TIPS

Bro, it's simple. Just see my code from my fragment. you will understand how to do it

   SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
   return sharedPreferences.getString(key, "");

Have you tried just using getActivity().getSharedPreferences()?

SharedPreferences prefs = getActivity().getSharedPreferences(
            "com.example.app", Context.MODE_PRIVATE);

Note: Can't ask this as question because of my low reputation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top