Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my android application, I am switching between the two set of images-(Set1, Set2) displayed in a GridView-A inside a Fragment-A.I am doing this on a button press using a boolean variable. Clicking on any GridView icon leads to another Fragment-B. The problem is when I press back button on Fragment-B , the Fragment-A is always loaded with the images of Set1 by default. I want the same set of images (Set1 or Set2 ) to be loaded on FramentA that were displayed before going to FragmentB.

Fragment Code

Java
public class GridViewFragment  extends Fragment{


    Context context;
    GridView GridMenu;
GridViewAdapter ga ;
    Button  Btn_Settings;
    Button Btn_lang_Ch;
    Button favoriteDuas;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        context = inflater.getContext();
        View view = inflater.inflate(R.layout.fragment_gridview, container, false); 
        ga = new GridViewAdapter(context);
        Btn_lang_Ch = (Button) view.findViewById(R.id.lng_ch);
        Btn_Settings = (Button) view.findViewById(R.id.button_settings);
        favoriteDuas = (Button) getActivity().findViewById(R.id.btn_favorite_duas);
        GridMenu =(GridView) view.findViewById(R.id.gridView1);
    float scalefactor = getResources().getDisplayMetrics().density * 150;
        @SuppressWarnings("deprecation")
        int number = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        int columns = (int) ((float) number / (float) scalefactor);
        GridMenu.setAdapter(ga);
        GridMenu.setNumColumns(columns);    
        return view;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

         if (savedInstanceState != null) {
                // Restore last state for checked position.
                mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
            }

        Btn_lang_Ch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                        ga.changeImages(!ga.imageSetChange);    
                        ga.Lang_Status();
            }
        });


 GridMenu.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int position,
                            long id) {


((MainActivity) context).loadSingleDuaFragment();

                }
        });

            super.onActivityCreated(savedInstanceState);

    }


}


GridView Adapter

Java
public class GridViewAdapter extends BaseAdapter {

    public boolean imageSetChange = false;
    public static boolean curr_lang=false;//english


    public Integer[] mThumbIds = {
            R.drawable.eng_pic1, R.drawable.eng_pic2,
            R.drawable.eng_pic3, R.drawable.eng_pic4,
            R.drawable.eng_pic5, R.drawable.eng_pic6,
            R.drawable.eng_pic7, R.drawable.eng_pic8,
            R.drawable.eng_pic9, R.drawable.eng_pic10,
            R.drawable.eng_pic11, R.drawable.eng_pic12,
            R.drawable.eng_pic13, R.drawable.eng_pic14,
            R.drawable.eng_pic15, R.drawable.eng_pic16,
            R.drawable.eng_pic17, R.drawable.eng_pic18,
            R.drawable.eng_pic19, R.drawable.eng_pic20,
            R.drawable.eng_pic21
           };

    public Integer[] mThumbIds1 = {
              R.drawable.urdu_dua1, R.drawable.urdu_dua2,
                R.drawable.urdu_dua3, R.drawable.urdu_dua4,
                R.drawable.urdu_dua5, R.drawable.urdu_dua6,
                R.drawable.urdu_dua7, R.drawable.urdu_dua8,
                R.drawable.urdu_dua9, R.drawable.urdu_dua10,
                R.drawable.urdu_dua11, R.drawable.urdu_dua12,
                R.drawable.urdu_dua13, R.drawable.urdu_dua14,
                R.drawable.urdu_dua15, R.drawable.urdu_dua16,
                R.drawable.urdu_dua17, R.drawable.urdu_dua18,
                R.drawable.urdu_dua19, R.drawable.urdu_dua20,
                R.drawable.urdu_dua21

};
    private Context mContext;

    public GridViewAdapter(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View MyView;
        if(convertView == null){

            LayoutInflater li=((Activity) mContext).getLayoutInflater();
            MyView =li.inflate(R.layout.menuitem, parent,false);

            }
            else{
                MyView =(View)convertView;
            }
        ImageView iv=(ImageView)MyView.findViewById(R.id.image);    

        if(imageSetChange){

        iv.setImageResource(mThumbIds1[position]);         

        }

        else 
        {
            iv.setImageResource(mThumbIds[position]);

        }

        return MyView;
    }


    public void changeImages(boolean change){
        this.imageSetChange  = change;
        notifyDataSetChanged();
     //   gf.Lang_Status();
    }

    public void Lang_Status()
    {
        // if curr_eng
        if(!curr_lang)    
            this.curr_lang= true; // change to urdu
        // if curr_urdu
        else 
        this.curr_lang= false;   // change to english

    }

}

So, can anybody help me out regarding what should be done to load the previous set of images on coming back to gridview fragment instead of having the default images of Set1.

Thank you.
Posted

1 solution

You can use SharedPreference to write and read values that you want to reload.

write:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.image_set), which);
editor.commit();


read:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String defaultValue = "set1";
String image_set = sharedPref.getString(getString(R.string.image_set), defaultValue);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900