0

I have Fragment with lots of TextViews. I put 4 of this Fragment in ViewPager Tab. I search a lot about saving Fragment state but all of them said to save individual elements data and then restore their data and reassign TextViews Values. But I need to save complete Fragment state with its View elements valus and restore it completely because reassigning TextViews value is time consuming. Could anyone tel me how to save complete Fragment state and restore it without need to reassign values?please Thank's

this is my Fragment Class that I want to store completely:

public class SampleFragment extends Fragment{

private TextView[] month_days;
private TextView[] weeks;
private TextView[] week_days;
private TableRow[] tb_row;
private TextView month_name;
protected boolean MONTH_NAME_VISIBILITY = false;
protected boolean WEEK_NUMBER_VISIBILITY = false;
private Configurations conf = new Configurations();
private View rootView;

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

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

    rootView = inflater.inflate(R.layout.month_view, container, false);



    initialize(this.getActivity());
    setDate();



    return rootView;
}

private void initialize(Context context) {
    month_days = new TextView[42];
    weeks = new TextView[6];
    week_days = new TextView[7];
    tb_row = new TableRow[7];
    month_name = (TextView) rootView.findViewById(R.id.month_name);

    Integer i, j, id;
    for (i = 0; i < 42; i++) {
        id = rootView.getResources().getIdentifier("tv" + (i + 1) + "", "id",conf.getPACKAGE_NAME() );
        month_days[i] = (TextView) rootView.findViewById(id);
    }

    for (i = 0; i < 6; i++) {
        id = rootView.getResources().getIdentifier("tvw" + (i + 1) + "", "id", conf.getPACKAGE_NAME());
        weeks[i] = (TextView) rootView.findViewById(id);
        weeks[i].setTextAppearance(activity, R.style.SmallFontBold);
    }

    for (i = 0; i < 7; i++) {
        id = rootView.getResources().getIdentifier("tvwd" + (i + 1) + "", "id", conf.getPACKAGE_NAME());
        week_days[i] = (TextView) rootView.findViewById(id);
        week_days[i].setText(conf.getDayName()[i]);
        week_days[i].setTextAppearance(activity, R.style.SmallFontBold);
        if (Arrays.asList(conf.getWEEK_FREE_DAYS_INDEX()).contains(String.valueOf(i)))
            week_days[i].setTextColor(Color.RED);
        id = getResources().getIdentifier("row" + (i + 1) + "", "id", conf.getPACKAGE_NAME());
        tb_row[i] = (TableRow) rootView.findViewById(id);
    } 
}

public void setDate() {

    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(conf.getCurrentCal().getFirstDayOfWeek());
    Integer i, j = 0, wn, backday, z;
    cal.set(conf.getCurrentCal().get(YEAR), conf.getCurrentCal().get(MONTH), 1);
    backday = ((cal.get(DAY_OF_WEEK)) - cal.getFirstDayOfWeek() + 7) % 7;

    wn = (int) (Math.ceil((backday + cal.getActualMaximum(DAY_OF_MONTH)) / 7.0));

    cal.add(DAY_OF_WEEK, -backday);

    for (i = 0; i < wn * 7; i++) {
        month_days[i].setText(String.valueOf(cal.get(DAY_OF_MONTH)));

        if (Arrays.asList(conf.getWEEK_FREE_DAYS_INDEX()).contains(String.valueOf(cal.get(DAY_OF_WEEK))))
            month_days[i].setTextColor(Color.RED);
        else
            month_days[i].setTextColor(Color.BLACK);

        if (cal.get(MONTH) != conf.getCurrentCal().get(MONTH))
            month_days[i].setTextAppearance(this.getActivity(), R.style.InActiveSmall);
        else
            month_days[i].setTextAppearance(this.getActivity(), R.style.SmallFont);

        cal.add(DAY_OF_MONTH, 1);
    }


    if (WEEK_NUMBER_VISIBILITY) {
        cal.set(conf.getCurrentCal().get(YEAR), conf.getCurrentCal().get(MONTH), 1);
        i = cal.get(WEEK_OF_YEAR);
        z = 0;
        for (j = 0; j < wn; j++) {
            weeks[j].setText(String.valueOf(cal.get(WEEK_OF_YEAR)));
            if ((i == 52) && (z == Calendar.DECEMBER))
                weeks[j].setText(String.valueOf(53));
            weeks[j].setVisibility(VISIBLE);
            i = cal.get(WEEK_OF_YEAR);
            z = cal.get(MONTH);
            cal.add(WEEK_OF_YEAR, 1);
        }
    } else {
        cal.set(conf.getCurrentCal().get(YEAR), conf.getCurrentCal().get(MONTH), 1);
        for (j = 0; j < 6; j++) {
            weeks[j].setVisibility(INVISIBLE);
            cal.add(WEEK_OF_YEAR, 1);
        }
    }

    for (j = 1; j <= 6; j++)
        if (j > wn)
            tb_row[j].setVisibility(INVISIBLE);
        else
            tb_row[j].setVisibility(VISIBLE);


    if (MONTH_NAME_VISIBILITY) {
        month_name.setText(conf.getMonthName()[conf.getCurrentCal().get(MONTH)]);
        month_name.setVisibility(VISIBLE);
    }
    else
        month_name.setVisibility(INVISIBLE);
}

public void setConf(Configurations conf) {
    this.conf.setCurrentCal(conf.getCurrentCal());
    this.conf.setCAL_FIRST_WEEK_DAY(conf.getCAL_FIRST_WEEK_DAY());
    this.conf.setDayName(conf.getDayName());
    this.conf.setMonthName(conf.getMonthName());
    this.conf.setPACKAGE_NAME(conf.getPACKAGE_NAME());
    this.conf.setTodayCal(conf.getTodayCal());
    this.conf.setWEEK_FREE_DAYS_INDEX(conf.getWEEK_FREE_DAYS_INDEX());
}

public void setmonth(Calendar cal){
    Calendar tcal = getInstance();
    tcal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
    this.conf.setCurrentCal(tcal);
}
}
Mohsen
  • 193
  • 2
  • 9
  • please post your code like what you have done? – Yograj Shinde Jan 20 '15 at 10:29
  • @Y.S he is not write any code they just want opinion for now or way to achieve his goal ..... – duggu Jan 20 '15 at 10:33
  • 1
    Basically this should be done by Fragment/FragmentManager itself. For Viewpagers you should use FragementStatePageAdapter as Adapter, which will save the Fragment State. (http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html) – Christopher Jan 20 '15 at 10:40
  • @ Christopher: Actually I need to save whole state and just put it in frame when needed. But these methods only save values and reassign them again to layouts. – Mohsen Jan 30 '15 at 08:02

1 Answers1

0

after a while no buddy answer my question! So searching a lot on different sites and finally I found a way to save fragments completely!! in this situation you should create fragment without UI (here and here) ! and set setRetainInstance(true) on fragments.

Mohsen
  • 193
  • 2
  • 9