Pjayness
Pjayness

Reputation: 365

Android: Using SharedPreferences with a Tab Layout

So I learned about making tabs in Android using FragmentActivity from THIS TUTORIAL. So now I have a FragmentActivity that holds 3 tabs, which means within those tabs are three different Fragments.

FragmentActivity class

public class ProductSingle_Activity extends FragmentActivity {

    ViewPager Tab;
    public TabPagerAdapter TabAdapter;
    ActionBar actionBar;
    Context ctx = this;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_single);

.... and so on

And I have two Fragments that become the two tabs of the FragmentActivity:

One Fragment:

public class details_frag_activity extends Fragment {

    Context ctx;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View android = inflater.inflate(R.layout.details_frag, container, false);

    }
}

So I have stored some information such as 'name' and 'description' in the SharedPreferences of the application.

SharedPreferences pref = ctx.getSharedPreferences("product_details", 0); 
//ctx being the context
Editor editor = pref.edit();
editor.putString("pname", name);
editor.commit();

I want to access those information through one of the Fragments like this:

public class details_frag_activity extends Fragment {

    Context ctx;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View android = inflater.inflate(R.layout.details_frag, container, false);

        SharedPreferences pref = ctx.getSharedPreferences("product_details", 0); 
//I cant access the ctx (Context). To avoid the error I just declared a local variable. I need to get the context of the FragmentActivity
        String pname = pref.getString("pname", null);

However I can't access the 'ctx' (Context) through the fragment. Is there a way to pass the context of the FragmentActivity to the Fragment Classes such that they can retrieve stored information from the SharedPreferences? OR is there a better way to access common information through the Fragments (Tabs)?

Upvotes: 1

Views: 1352

Answers (3)

user4380047
user4380047

Reputation:

As mentioned in the previous answer, you can simply use:

SharedPreferences pref = getActivity().getSharedPreferences("product_details", 0); 

Upvotes: 2

J.K
J.K

Reputation: 2393

You can override onAttach method in your details_frag_activity and create the ProductSingle_Activity object inside details_frag_activity class use this...

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    this.activity = (ProductSingle_Activity) activity;
}

or simply try

 SharedPreferences pref = getActivity().getSharedPreferences("product_details", 0); 

Upvotes: 2

Rathan Kumar
Rathan Kumar

Reputation: 2577

in fragement class one method is there by using that you can get Context.

private Activity activity;
@Override
public void onAttach(Activity activity) {
    this.activity = activity;
    super.onAttach(activity);
}

Just assign that method parameter to a field then you can access this activity reference as context where ever you want. because Activity is Subclass of Context.

Upvotes: 1

Related Questions