title: OnDetach/onAttach fragment recreate fragment activity
body: <p>I want to detach/attach my fragments, but how to set, that fragment not recreate, after I attach.</p>
  
  <p>In fragment I have WebView; when I select and unselect tabs, webview load startpage.</p>
  
  <p>There is my code:</p>
  
  <pre><code>  public class MainActivity extends Activity implements OnClickListener, OnMenuItemClickListener {
  
  
      ActionBar bar;
      View v;
  
       public static TextView tilt;
      LayoutInflater inflater;
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.main);
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
          onAddTab();
          View v=getLayoutInflater().inflate(R.layout.action_bar, null);
           ImageButton im = (ImageButton)v.findViewById(R.id.tab_toggle);
          im.setOnClickListener(this);
          getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_layout));
          getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
          getActionBar().setCustomView(v);
          onToggleTabs();
  
  
  
          }
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
          switch (v.getId()){
          case R.id.tab_toggle:
  
              onAddTab();
  
              break;
          }
  
      }
  
      public void onAddTab() {
          final ActionBar bar = getActionBar();
          View v=getLayoutInflater().inflate(R.layout.layout_tab, null);
          tilt = (TextView)v.findViewById(R.id.tit_le);
  
          ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT); 
          lp.gravity = Gravity.CENTER;
          v.setLayoutParams(lp);
          closetab = (ImageButton)v.findViewById(R.id.close);
          closetab.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  onRemoveTab();
  
  
              }
          });
  
          bar.addTab(bar.newTab()
                  .setCustomView(v)
                  .setTabListener(new TabListener&lt;Web&gt;(this, "Tag A", Web.class)));
  
      }
  
  
  
      public void onRemoveTab() {
          final ActionBar bar = getActionBar();
          Tab tab = bar.getSelectedTab();
  
              bar.removeTab(tab);
  
      }
  
      public void onToggleTabs() {
          final ActionBar bar = getActionBar();
  
  
              bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
  
      }
  
      public void onRemoveAllTabs(View v) {
          getActionBar().removeAllTabs();
      }
  
  
  
  
      public static class TabListener&lt;T extends Fragment&gt;  implements ActionBar.TabListener{
  
         private final Activity myActivity;
         private final String myTag;
         private final Class&lt;T&gt; myClass;
  
         public TabListener(Activity activity, String tag, Class&lt;T&gt; cls) {
             myActivity = activity;
             myTag = tag;
             myClass = cls;
         }
  
   @Override
   public void onTabSelected(Tab tab, FragmentTransaction ft) {
  
    Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
  
    // Check if the fragment is already initialized
          if (myFragment == null) {
              // If not, instantiate and add it to the activity
              myFragment = Fragment.instantiate(myActivity, myClass.getName());
              ft.add(R.id.fragment0, myFragment, myTag);
          } else {
              // If it exists, simply attach it in order to show it
              ft.attach(myFragment);
          }
  
   }
  
   @Override
   public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  
    Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
  
    if (myFragment != null) {
              // Detach the fragment, because another one is being attached
              ft.detach(myFragment);
  
          }
  
   }
  
   @Override
   public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
  
   }
      }
  </code></pre>
  
  <p>There is Fragment:</p>
  
  <pre><code>public class Web extends Fragment implements OnLongClickListener, OnClickListener{
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
  
      super.onCreate(savedInstanceState);
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      c=this.getActivity();
  
      v = inflater.inflate(R.layout.activity_main, container, false);
      return v;
  }
  
  @SuppressWarnings("deprecation")
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onActivityCreated(savedInstanceState);
  setRetainInstance(true);
  
  
  wv = (WebView)v.findViewById(R.id.wv);
  wv.setWebChromeClient(new WebChromeClient(){
      @Override
      public void onProgressChanged(WebView view, int progress) {
          // TODO Auto-generated method stub
          super.onProgressChanged(view, progress);
  
                 if(progress &lt; 100 &amp;&amp; pr.getVisibility() == ProgressBar.GONE){
                     pr.setVisibility(ProgressBar.VISIBLE);
  
                 }
                 pr.setProgress(progress);
                 if(progress == 100) {
                     pr.setVisibility(ProgressBar.GONE);
  
                 }
              }
  });
  wv.setWebViewClient(new MyWebViewClient());
  
  
  
  
  
  
  
  
  
  
  
  wv.loadUrl("http://www.google.com");
  wv.setOnLongClickListener(this);}
  </code></pre>