简体   繁体   中英

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)?

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); 

如上一个答案所述,您可以简单地使用:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM