title: Replacing Fragment with fragment in activity
body: <p>I have an <code>Activity</code> that has a <code>Fragment</code> (Frag A) added to it in its <code>onCreate</code> method. This <code>Fragment</code> contains the UI elements i would like the activity to display on default. On button click in frag A, i would like to replace Frag A with another fragment(Frag B) that shows a progress bar. If there is a configuration while Frag B is showing, Frag(A) should not be added when the activity's onCreate method is called. </p>
  
  <p>How can i achieve this?</p>
  
  <p>Just like its done in this facebook app:</p>
  
  <p><img src="https://i.stack.imgur.com/KD3Yv.png" alt="First Screen showing UI ">
  <img src="https://i.stack.imgur.com/YPnvy.png" alt="Second sreeen showing progress bar after button click in "></p>
  
  <p>I've tried a lot of methods:</p>
  
  <ol>
  <li>First i added the UI elements directly in the activity's layout along with an empty framelayout, so on button click i hide the UI elements and do a <code>Fragment</code> transaction to put my fragment containing my progress bar in the framelayout, this works fine but if there is configuration change the activity is recreated and i am back to square one.    </li>
  </ol>
  
  <p>This is what i have now.</p>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>    public class MainActivity extends FragmentActivity  {
  Button button;
  private ProgressDialog mpDialog;
  boolean running;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      MainFragment fragment = new MainFragment();
          FragmentTransaction fragmentTransaction=    getSupportFragmentManager().beginTransaction();
          fragmentTransaction.add(R.id.wrapper, fragment).commit();
  
  }
  
  
  
  public void Click() {
      TaskFragment taskFragment = new TaskFragment();
      getSupportFragmentManager().beginTransaction().replace(R.id.wrapper, taskFragment).commit();
  
  }
  
  
   }
  </code></pre>
  
  <p><strong>MainFragment(Holding UI elements)</strong>     </p>
  
  <pre><code>    public class MainFragment extends Fragment {
  
  View fView;
  ProgressBar bar;
  TextView textView;
  Button button;
  MainActivity activity = new MainActivity();
  @Override
  public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setRetainInstance(true);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      fView = inflater.inflate(R.layout.mainfragment, container, false);
      textView = (TextView) fView.findViewById(R.id.textview);
      button = (Button) fView.findViewById(R.id.button);
      clicked();
      return fView;
  }
  
  
  private void clicked() {
      // TODO Auto-generated method stub
      button.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
  
              activity.Click();
          }
      });
  
  }
  
     }
  </code></pre>
  
  <p><strong>TaskFragment(Showing progress bar)</strong></p>
  
  <pre><code>   public class TaskFragment extends Fragment{
  
  View fview;
  ProgressBar bar;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      // TODO Auto-generated method stub
  
      fview = inflater.inflate(R.layout.progressfragment, container, false);
      bar = (ProgressBar) fview.findViewById(R.id.sendingSmsProgress);
      return fview;
  }
  
   }
  </code></pre>
  
  <p><strong>The Error i get when i try to switch fragments</strong> </p>
  
  <pre><code>  03-14 05:41:44.457: E/AndroidRuntime(11049): FATAL EXCEPTION: main
    03-14 05:41:44.457: E/AndroidRuntime(11049): java.lang.IllegalStateException: Activity has been destroyed
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at com.example.test.MainActivity.Click(MainActivity.java:41)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at com.example.test.MainFragment$1.onClick(MainFragment.java:47)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.view.View.performClick(View.java:4102)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.view.View$PerformClick.run(View.java:17085)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.os.Handler.handleCallback(Handler.java:615)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.os.Handler.dispatchMessage(Handler.java:92)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.os.Looper.loop(Looper.java:155)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.app.ActivityThread.main(ActivityThread.java:5454)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at java.lang.reflect.Method.invoke(Method.java:511)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
    03-14 05:41:44.457: E/AndroidRuntime(11049):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>