title: How to load Fragment only once and save its state?
body: <p>I have one mainActivity from where i am calling the list of fragments.</p>
  
  <p>Now,in this list i have one fragment called MyAccount which display the list of Account Details,using webservice.</p>
  
  <p>What i want is i want to load the myAccount Details only once(first time) fragment is loaded,not every time.(which is happening right now).Moreover, i want to set one refresh button in action bar,so on click of that refresh button/image the account details will be load and display.</p>
  
  <p>Here is my Implementation ::</p>
  
  <p><strong>getFragement() in Main Acitivty ::</strong> </p>
  
  <pre><code>private List&lt;Fragment&gt; getFragments()
      {
          List&lt;Fragment&gt; fList = new ArrayList&lt;Fragment&gt;();
  
  
              fList.add(Fragment.instantiate(this,Fragment1.class.getName()));
  
  
              fList.add(Fragment.instantiate(this,Fragment2.class.getName()));
  
  
  
              fList.add(Fragment.instantiate(this,Fragment3.class.getName()));
  
  
  
          fList.add(MyAccountFragment.newInstance(this,"My Account"));
          fList.add(ReportFragment.newInstance(this,"Reports"));
          fList.add(SettingsListFragment.newInstance(this,"Settings"));
  
          return fList;
      }
  </code></pre>
  
  <p><strong>Calling the adapter ::</strong> </p>
  
  <pre><code>actionbartabmenuAdapter = new ActionBarTabMenuAdapter(getSupportFragmentManager(), fragments,this);
  awesomePager.setAdapter(actionbartabmenuAdapter);
  </code></pre>
  
  <p><strong>Adapter ::</strong> </p>
  
  <pre><code>private class ActionBarTabMenuAdapter extends FragmentPagerAdapter  {
          Activity context;
          Context ctx;
          private List&lt;Fragment&gt; fragments;
  
          public ActionBarTabMenuAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments,Context ctx,)
          {
              super(fm);
              this.context=(Activity) ctx;
  
              this.fragments = fragments;
          }
  
          @Override
          public int getCount() 
          {
              return fragments.size();
          }
          @Override
          public Fragment getItem(int position) {
  
              System.out.println("position of fragment--"+position);
              return this.fragments.get(position);
          }
      }
  </code></pre>
  
  <p><strong>onTabSelected/Reselected Method ::</strong></p>
  
  <pre><code>@Override
      public void onTabReselected(Tab tabposition, FragmentTransaction fragmentposition) {
          System.out.println("Tab Reselected method");
  
      }
  
      @Override
      public void onTabSelected(Tab tabposition, FragmentTransaction fragmentposition) {
  
          awesomePager.setCurrentItem(tabposition.getPosition());
      }
  
      @Override
      public void onTabUnselected(Tab tabposition, FragmentTransaction fragmentposition) {
          System.out.println("Tab unselected method");
  
      }
  </code></pre>
  
  <p><strong>MyAccountFragment ::</strong> </p>
  
  <pre><code>public class MyAccountFragment extends SherlockFragment{
  
      static MyAccountFragment f ;
      ListView list_subscriberInfo;
      ProgressDialog pd = null;
      MyAccountInfo myAccountInfo = new MyAccountInfo();
      private MyApplication appContext;
      static Context context;
  
      public static MyAccountFragment newInstance(Activity ctx,String string)
      {
          f = new MyAccountFragment();
          context=ctx;
          return f;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          getSherlockActivity().supportInvalidateOptionsMenu(); // now onCreateOptionsMenu(...) is called again
          this.setHasOptionsMenu(true);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
      {
  
          View v = inflater.inflate(R.layout.my_account, container, false);
          list_subscriberInfo = (ListView)v.findViewById(R.id.list_subscriberInfo);
  
          return v;
      }
  
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
      {
          super.onCreateOptionsMenu(menu, inflater);
          inflater.inflate(R.menu.menu_new, menu);
      }
  
      public void onPrepareOptionsMenu(Menu menu) 
      {
          super.onPrepareOptionsMenu(menu);
      }
      public void setUserVisibleHint(boolean isVisibleToUser) 
      {
          super.setUserVisibleHint(isVisibleToUser);
          pd=new ProgressDialog(context);
          if (isVisibleToUser) 
          {
              pd =ProgressDialog.show(context,null, "Please Wait..", true);
              pd.setContentView(R.layout.progress);
              Thread thread = new Thread(getPersonalInfo);
              thread.start();
          }
          else
          {
              if(pd.isShowing())
              {
                  pd.dismiss();
              }
          }
      }
  }
  </code></pre>
  
  <p>Please note in myAccountFragment i have set setUserVisibleHint() to dislpay the account info only when this fragment get visibility true.</p>
  
  <p>I know that my Problem's solution is setOffScreenLimit() but i don't know where/how to set it my implementation.</p>
  
  <p>But how to use both means visible the my account only when fragment get visible and also not load the fragment every time.? Load only only when the refresh button is clicked in action bar(second time).</p>
  
  <p>Hope i am clear.</p>
  
  <p>Thanks in advance...</p>
  
  <p><strong>EDIT::</strong></p>
  
  <p>Applying LuksProg's Logic...</p>
  
  <p><strong>MyContainerActivity ::</strong></p>
  
  <pre><code>MyAccountFragment mSavedAccount;
  
  public void cacheAccount(MyAccountFragment account) {
          mSavedAccount = account;
      }
  
      public Object getCachedAccount() {
          return mSavedAccount;
      }
  </code></pre>
  
  <p><strong>MyAccountFragment ::</strong> </p>
  
  <pre><code>@Override
      public void onActivityCreated(Bundle savedInstanceState) 
      {
          super.onActivityCreated(savedInstanceState);
          containerActivity = (ButtonPayActivity) getActivity();
          pd=new ProgressDialog(context);
  
          if (containerActivity.getCachedAccount() != null)
          {
              //Do nothing
              if(pd.isShowing())
              {
                  pd.dismiss();
              }
          }
          else
          {
              // the data isn't available so this must be a fresh start
              // start the thread, remember to use act.cacheAccount() when the thread finishes
              pd =ProgressDialog.show(context,null, "Please Wait..", true);
              pd.setContentView(R.layout.progress);
              Thread thread = new Thread(getPersonalInfo);
              thread.start();
          }
      }
  </code></pre>
  
  <p><strong>EDIT 2 ::</strong></p>
  
  <p>After applying the logic for progress dialog i faced the following issues.</p>
  
  <p>what i have done is show and cancel the progress dialog but after i move to the very next tab of my Account my app gets crashed...</p>
  
  <pre><code>awesomePager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
  
              @Override
              public void onPageSelected(int position)
              {
                  //supportInvalidateOptionsMenu();
                  actionBar.setSelectedNavigationItem(position);
                   if (position == 4)
                   {
                          MyAccountFragment df = (MyAccountFragment) actionbartabmenuAdapter.getItem(position);
                      if (df.getThread() != null &amp;&amp; df.getThread().isAlive()) 
                      {
                          df.showDialog();
                      }
                      else
                      {
                          df.cancelDialog();
                      }
                  }
               }
  
              @Override
              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                  super.onPageScrolled(position, positionOffset, positionOffsetPixels);
              }
  
              @Override
              public void onPageScrollStateChanged(int state) {
                  super.onPageScrollStateChanged(state);
              }
          });
  </code></pre>
  
  <p><strong>myAccountFragment ::</strong> </p>
  
  <pre><code>@Override
      public void onActivityCreated(Bundle savedInstanceState) 
      {
          super.onActivityCreated(savedInstanceState);
          containerActivity = (ButtonPayActivity) getActivity();
          pd=new ProgressDialog(context);
  
          if (containerActivity.getCachedAccount() != null)
          {
  
              if(pd.isShowing())
              {
                  pd.dismiss();
              }
          }
          else
          {
  
              pd =ProgressDialog.show(context,null, "Please Wait..", true);
              pd.setContentView(R.layout.progress);
              thread = new Thread(getPersonalInfo);
              thread.start();
          }
      }
  
      public void showDialog() 
      {
          if (pd != null) 
          {
              pd.show(); // the dialog will be started from the OnPageChangeListener only
          }
      }
  
      public void cancelDialog() 
      {
          if (pd != null) 
          {
              pd.dismiss();
          }
      }
  
      public Thread getThread() 
      {
          return thread;
      }
  </code></pre>
  
  <p><strong>Logcat ::</strong> </p>
  
  <pre><code>12-24 20:31:20.076: E/AndroidRuntime(15596): FATAL EXCEPTION: main
  12-24 20:31:20.076: E/AndroidRuntime(15596): java.lang.ClassCastException: com.example.app.home.ReportFragment
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at com.example.app.ContainerActivity$1.onPageSelected(ContainerActivity.java:151)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:438)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:405)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:386)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at com.example.app.ContainerActivity.onTabSelected(ContainerActivity)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at com.actionbarsherlock.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:531)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at com.actionbarsherlock.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:912)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at com.actionbarsherlock.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:504)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at android.view.View.performClick(View.java:2485)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at android.view.View$PerformClick.run(View.java:9080)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at android.os.Handler.handleCallback(Handler.java:587)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at android.os.Handler.dispatchMessage(Handler.java:92)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at android.os.Looper.loop(Looper.java:130)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at android.app.ActivityThread.main(ActivityThread.java:3683)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at java.lang.reflect.Method.invokeNative(Native Method)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at java.lang.reflect.Method.invoke(Method.java:507)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  12-24 20:31:20.076: E/AndroidRuntime(15596):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Still Problem persists and just for curiosity i apply the following thing in my code with no success.
  Now i get the progress dialog earlier means before the myAccount Fragment get visibity,so i applied <code>onActivityCreated()</code> to in <code>setUserVisibleHint()</code> and keep the rest of the thing as it is.</p>
  
  <p>But then also my app gets crashed and i got following log..</p>
  
  <p><strong>Logcat ::</strong> </p>
  
  <pre><code>12-24 20:22:26.125: E/AndroidRuntime(14861): FATAL EXCEPTION: main
  12-24 20:22:26.125: E/AndroidRuntime(14861): java.lang.NullPointerException
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at com.example.app.home.MyAccountFragment.setUserVisibleHint(MyAccountFragment.java:183)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:102)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:433)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:405)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:386)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at com.example.app.ContainerActivity.onTabSelected(ContainerActivity.java:385)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at com.actionbarsherlock.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:531)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at com.actionbarsherlock.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:912)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at com.actionbarsherlock.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:504)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.view.View.performClick(View.java:2485)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.view.View$PerformClick.run(View.java:9080)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.os.Handler.handleCallback(Handler.java:587)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.os.Handler.dispatchMessage(Handler.java:92)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.os.Looper.loop(Looper.java:130)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at android.app.ActivityThread.main(ActivityThread.java:3683)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at java.lang.reflect.Method.invokeNative(Native Method)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at java.lang.reflect.Method.invoke(Method.java:507)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  12-24 20:22:26.125: E/AndroidRuntime(14861):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>