title: Android Fragments (with WebView) - Activity gets Destroyed
body: <p>I am using a Tab based Activity in my App and I want to implement a Fragment Stack with WebViews. In these WebViews I want to catch URL requests and replace the current WebView Fragment with a new one. Unfortunately I always get <code>Activity was destroyed Exception</code> but don't know why.</p>
  
  <p>Can you please give me some hints how to fix this?</p>
  
  <p>I use the following code (At the end of the code sample you will recognise the method <code>handleUrlRequest</code> where I want to replace the current Fragment):</p>
  
  <pre><code>public class FragmentStackSupport extends SherlockFragmentActivity {
  int mStackLevel = 1;
  
  public static int THEME = R.style.Theme_Sherlock_Light;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      setTheme(THEME); //Used for theme switching in samples
      super.onCreate(savedInstanceState);
      setContentView(R.layout.fragment_stack);
  
  
      if (savedInstanceState == null) {
          // Do first time initialization -- add initial fragment.
          Fragment newFragment = Web.newInstance(mStackLevel);
          FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
          ft.add(R.id.simple_fragment, newFragment).commit();
      } else {
          mStackLevel = savedInstanceState.getInt("level");
      }
  }
  
  @Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      outState.putInt("level", mStackLevel);
  }
  
  
  public void addFragmentToStack() {
      mStackLevel++;
  
      // Instantiate a new fragment.
      Fragment newFragment = Web.newInstance(mStackLevel);
  
      // Add the fragment to the activity, pushing this transaction
      // on to the back stack.
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
      ft.replace(R.id.simple_fragment, newFragment);
      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
      ft.addToBackStack(null);
      ft.commit();
  }
  
  
  
  public static class Web extends SherlockFragment {
      int mNum;
  
      /**
       * Create a new instance of CountingFragment, providing "num"
       * as an argument.
       */
      static Web newInstance(int num) {
          Web f = new Web();
  
          // Supply num input as an argument.
          Bundle args = new Bundle();
          args.putInt("num", num);
          f.setArguments(args);
  
          return f;
      }
  
      /**
       * When creating, retrieve this instance's number from its arguments.
       */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mNum = getArguments() != null ? getArguments().getInt("num") : 1;
      }
  
      /**
       * The Fragment's UI is just a simple text view showing its
       * instance number.
       */
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.hello_world, container, false);
          View wv = v.findViewById(R.id.webview);
          WebView webView = ((WebView)wv);
  
          webView.loadUrl("http://www.google.at");
          webView.setWebViewClient(new MyWebViewClient());
  
  
  
          return v;
      }
  }
  
  public static class MyWebViewClient extends WebViewClient {
  
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String urlString) {
  
          handleURLRequest("modal");
          return false;
      }
  
  
      private void handleURLRequest(String transitionStyle){
  
          // here I want to replace the Fragment with a new one.
  
      }
  }
  
  }
  </code></pre>