0

In an android application,I have multiple Fragments, In one of the fragment (Fragment A), I am switching between two set of images in a GridView on button click using a Boolean variable. Th GridView Item Click leads to a second Fragment (Fragment B). On pressing Back button on Fragment B,gridview inside Fragment A is loaded with the default set of images, not the set images before going on the next fragment Fragment B.

Please guide how can I restore the previous set of Images not the default set of Images ?

In MainActivity, I have implemented on BackPressed like this:

@Override
    public void onBackPressed() {
        if (frag instanceof SettingsFragment)
            loadGridViewFragment();
        else if (frag instanceof SingleDuaViewPagerFragment) {
            boolean keyIsFavSel = new SharedPreferencesSupplication().read(SingletonClass.keyIsFavSelected, false);
            if (keyIsFavSel)
                loadDuasListFragment();
            else
                loadGridViewFragment();
        }
        else if (frag instanceof DuasListFragment) {
            boolean keyIsFavSel = new SharedPreferencesSupplication().read(SingletonClass.keyIsFavSelected, false);
            if (keyIsFavSel)
                loadGridViewFragment();
            else
                loadSingleDuaFragment();
        }
        else
            super.onBackPressed();
    }

OnCreate Method of GridViewFragment.java

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    //  fragment = new GridViewFragment();
        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) view.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);    
        setRetainInstance(true);
        return view;

    }
user2011302
  • 391
  • 1
  • 4
  • 22

1 Answers1

2

you should call setRetainInstance(true) in onCreate method of fragment or save and load proper values in onSaveInstanceState and onRetainInstanceState

EDIT: I usually do something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    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);
        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);
    }
    else {
       // remove view from previously attached ViewGroup
       ViewGroup parent = (ViewGroup) view.getParent();
       parent.removeView(view);
    }    
    return view;
}
vaske
  • 398
  • 1
  • 3
  • 13
  • I have called setRetainInstance(true) in OnCreate but still default images are being loaded. What values should be saved? Please explain. – user2011302 Apr 22 '14 at 11:26
  • If you called setRetainInstanceState(true), then when you get back to previous fragment, only onCreateView is called ... which means that you should return existing view in onCreateView. if it is not null i usualy just remove view from its parent and return existing instance of view. save view to global variable when creating it in onCreateView and return existing one if it exists. – vaske Apr 22 '14 at 12:58
  • and you are calling setRetainInstanceState in onCreateView, not in onCreate – vaske Apr 22 '14 at 13:06
  • OnCreateView is having all the components to draw UI then what should be done in OnCreate ? – user2011302 Apr 23 '14 at 03:52
  • Thanks for helping but still no difference. – user2011302 Apr 23 '14 at 06:27
  • I have elaborated the scenario. Please see and help... http://stackoverflow.com/questions/23150271/saving-gridview-state-in-a-fragment-android-on-back-button – user2011302 Apr 23 '14 at 06:45