2

I have a Tabhost with 5 tab: tab1, tab2, tab3, tab4, tab5
and all of them have same fragment with different content.
My fragment onCreateView method have these code:

switch(tabtag){
   case 1:
       loaddata1();
       break;
   case 2:
       loaddata2();
       break;
   case 3:
       loaddata3();
       break;
   case 4:
       loaddata4();
       break;
   case 5:
       loaddata5();
       break;
}

I loaded related data correctly, but I want load data for each tab only once time.
I used from onSaveInstanceState and onActivityCreated methods:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("1", isFirsttime1);
    outState.putBoolean("2", isFirsttime2);
    outState.putBoolean("3", isFirsttime3);
    outState.putBoolean("4", isFirsttime4);
    outState.putBoolean("5", isFirsttime5);
    outState.putBoolean("6", isFirsttime6);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        // Restore last state for checked position.
        isFirsttime1 = savedInstanceState.getBoolean("1");
        isFirsttime2 = savedInstanceState.getBoolean("2");
        isFirsttime3 = savedInstanceState.getBoolean("3");
        isFirsttime4 = savedInstanceState.getBoolean("4");
        isFirsttime5 = savedInstanceState.getBoolean("5");
        isFirsttime6 = savedInstanceState.getBoolean("6");

    }
}

So in onCreateView I added this code in each switch case:

if (isFirsttime){
   loaddata();
}
else{
   //use from my view state
}

but it does not work like that and I think because I use from same fragment again, onSaveInstanceState method not called.

How can I solved it?
Thanks in advance.

Naruto Uzumaki
  • 2,019
  • 18
  • 37

0 Answers0