Saving GridView state in a Fragment (Android) on Back Button

我是研究僧i 提交于 2019-12-08 06:45:26

问题


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.

EDITED: Fragment Code

  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) {
        super.onCreate(savedInstanceState);

        //fragment = new GridViewFragment();
        if (view == null) {
            context = inflater.getContext();
            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) view.findViewById(R.id.btn_favorite_duas);
            GridMenu = (GridView) view.findViewById(R.id.gridView1);
            GridMenu.setAdapter(ga);

        } else {
            // remove view from previously attached ViewGroup
            ViewGroup parent = (ViewGroup) view.getParent();
            parent.removeView(view);
        }

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setRetainInstance(true);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        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

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

    }

}

EDIT: MainActivity

public void loadGridViewFragment() {
        if (activityActive) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if(getSupportFragmentManager().findFragmentByTag(GridViewFragment.TAG)!=null){
                ft.replace(R.id.fl_view, frag,GridViewFragment.TAG);
            }   
            else
            {
            frag = new GridViewFragment();
            ft.replace(R.id.fl_view, frag,GridViewFragment.TAG);
            }
            ft.commit();
        }
    }

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.

来源:https://stackoverflow.com/questions/23150271/saving-gridview-state-in-a-fragment-android-on-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!