title: Unable to destroy activity,IllegalStateException:activity has been destroyed android fragments
body: <p>I have created a project where a webservice is called to return a list of colleges in a listview.On clicking a specific college, a new fragment activity containing two fragments are shown.The first fragment contains details in textview and a map.The second fragment contains news details of the specific college.When i press back from the fragment activity(containing two fragments), i get the error in the title.</p>
  
  <p>The fragmentmain.java</p>
  
  <pre><code>package com.igloo.collegefinder;
  
  
  
  
  import com.igloo.classes.College_data;
  
  import android.os.Bundle;
  import android.os.Handler;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.LinearLayout;
  import com.google.android.gms.maps.SupportMapFragment;
  
  public class FragmentMain extends FragmentActivity {
      LinearLayout footer;
      Button btndetails,btncollegenews;
      College_data college_item;
      Fragment fr;
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragment_main);
          footer=(LinearLayout) findViewById(R.id.footer);
          btndetails=(Button) footer.findViewById(R.id.btndetails);
          btncollegenews=(Button) footer.findViewById(R.id.btncollegenews);
          college_item= (College_data) getIntent().getExtras().getSerializable("college_data");
  
  //      Fragment fragment = new Fragment();
          final Bundle bundle = new Bundle();
          bundle.putSerializable("college_data",college_item);
  
          final CollegeDetails fragobj1=new CollegeDetails();
          fragobj1.setArguments(bundle);
  
          final NewsDetails fragobj2=new NewsDetails();
          fragobj2.setArguments(bundle);
  
                  if(!isFinishing())
                  {
                  fr=fragobj1;
                  android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
                   android.support.v4.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
                   fragmentTransaction.replace(R.id.fragment_place,fr); 
                   fragmentTransaction.commitAllowingStateLoss();
                   fm.executePendingTransactions();
                   //fragmentTransaction.commit();
                  }
  
           btndetails.setOnClickListener(new OnClickListener() {
  
              @Override
  
  
              public void onClick(View arg0) {
  
                          if(!isFinishing())
                          {
                              try{
                          fr=new CollegeDetails();
                          fr.setArguments(bundle);
                           android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
                           android.support.v4.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
                           fragmentTransaction.replace(R.id.fragment_place,fr);     
                           fragmentTransaction.commitAllowingStateLoss();
                           //fragmentTransaction.commit();
                           fm.executePendingTransactions();
                              }catch(Exception e){
                                  e.printStackTrace();
                              }
  
  
                      }
  
  
  
              }
          });
           btncollegenews.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
  
                          if(!isFinishing())
                          {
                          fr=new NewsDetails();
                          fr.setArguments(bundle);
                           android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
                           android.support.v4.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
                           fragmentTransaction.replace(R.id.fragment_place,fr); 
                           fragmentTransaction.commitAllowingStateLoss();
                           fm.executePendingTransactions();
                           //fragmentTransaction.commit();
                          }
  
                      }
                  });
  
  
  
  
  
      }
  
      @Override
      public void onBackPressed() {
          try{
          finish();
          }catch(Exception e){
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>The first fragment CollegeDetails.java</p>
  
  <pre><code>package com.igloo.collegefinder;
  
  
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import com.google.android.gms.maps.CameraUpdateFactory;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.SupportMapFragment;
  import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;
  import com.igloo.classes.College_data;
  import com.igloo.classes.News_data;
  import com.igloo.collegefinder.CollegeMain.RetrieveColleges;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.Handler;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.view.InflateException;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ListView;
  import android.widget.Spinner;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class CollegeDetails extends Fragment {
  
      TextView txtcollegename,txtcollegeaddress,txtcollegepho,txtcollegelatitude,txtcollegelongitude,txtcollegeimage,txtcollegenews;
      String college_name,course_name;
      ProgressDialog progressDialog;
      String URL="http://footballultimate.com/finder/index.php/api/collegeDataWithoutFilter";
      HttpResponse response;
      String result,news_value="";
      JSONObject jsonobject;
      JSONArray college_data;
      College_data college_item;
      Bundle b;
      View v;
      FragmentManager fragManager;
      private FragmentActivity myContext;
       // Google Map
      private GoogleMap googleMap;
      SupportMapFragment mMapFragment;
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
  
          v=inflater.inflate(R.layout.college_details, null, false);
  
          txtcollegename=(TextView)v.findViewById(R.id.txtcollegename);
          txtcollegeaddress=(TextView)v.findViewById(R.id.txtcollegeaddress);
          txtcollegepho=(TextView)v. findViewById(R.id.txtcollegepho);
          txtcollegelatitude=(TextView)v. findViewById(R.id.txtcollegelatitude);
          txtcollegelongitude=(TextView)v. findViewById(R.id.txtcollegelongitude);
          txtcollegeimage=(TextView)v. findViewById(R.id.txtcollegeimage);
          txtcollegenews=(TextView)v. findViewById(R.id.txtcollegenews);
          //college_item= (College_data)getActivity().getIntent().getExtras().getSerializable("college_data");
          college_item=(College_data) getArguments().getSerializable("college_data");
  
  
          txtcollegename.setText(college_item.collegename);
          txtcollegeaddress.setText(college_item.collegeaddress);
          txtcollegepho.setText(college_item.collegephone);
          txtcollegelatitude.setText(college_item.collegelatitude);
          txtcollegelongitude.setText(college_item.collegelongitude);
          txtcollegeimage.setText(college_item.collegeimage);
  //      for(News_data item : college_item.news_list){
  //          news_value=news_value+item.news+" ";
  //      }
  //      txtcollegenews.setText(news_value);
  
           try {
                  // Loading map
                  initilizeMap();
  
              } catch (InflateException e) {
                  e.printStackTrace();
              }
  return v;
      }
      @Override
      public void onAttach(Activity activity) {
           myContext=(FragmentActivity) activity;
              super.onAttach(activity);
      }
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onActivityCreated(savedInstanceState);
  
      }
      private void initilizeMap() {
          if (googleMap == null) {
               fragManager = myContext.getSupportFragmentManager();
  //          mMapFragment = SupportMapFragment.newInstance();
  //            FragmentTransaction fragmentTransaction =
  //                     getChildFragmentManager().beginTransaction();
  //             fragmentTransaction.add(R.id.map, mMapFragment);
  //             fragmentTransaction.commit(); 
  
              googleMap = ( (SupportMapFragment) fragManager.findFragmentById(
                      R.id.map)).getMap();
  
  
  
          }
              // check if map is created successfully or not
              if (googleMap == null) {
                  Toast.makeText(getActivity(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
              }
              else
              {
                  //googleMap.setMyLocationEnabled(true);
           // latitude and longitude
              double latitude =Double.parseDouble(college_item.collegelatitude);
              double longitude =Double.parseDouble(college_item.collegelongitude);
  
              // create marker
              MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title(college_item.collegename);
              marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
              // adding marker
              googleMap.addMarker(marker);
  
  
  
  
             // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
              googleMap.getUiSettings().setMyLocationButtonEnabled(false);
  
  
  
  
  
              googleMap.getUiSettings().setRotateGesturesEnabled(false);
              googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),12.0f));
  
              }
          }
      @Override
      public void onDestroyView() 
           {
              super.onDestroyView(); 
  
                  Fragment fragment = (fragManager.findFragmentById(R.id.map));  
                  FragmentTransaction ft = (myContext).getSupportFragmentManager().beginTransaction();
                  ft.remove(fragment);
                 ft.commitAllowingStateLoss();
                  //ft.commit();
  
              }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
  
      }
      @Override
      public void onDestroy() {
  
          SupportMapFragment f = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
          if (f.isResumed()){ 
              getFragmentManager().beginTransaction().remove(f).commit();
          }
  
          super.onDestroy();
      }
  }
  </code></pre>
  
  <p>The second fragment NewsDetails.java</p>
  
  <pre><code>package com.igloo.collegefinder;
  
  import com.igloo.classes.College_data;
  import com.igloo.classes.News_data;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  
  public class NewsDetails extends Fragment {
      View v;
      TextView txtnewsfinal;
      College_data college_item;
      String news_value="";
      @Override
         public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
          v=inflater.inflate(R.layout.news_details, null, false);
          college_item=(College_data) getArguments().getSerializable("college_data");
  
          txtnewsfinal=(TextView) v.findViewById(R.id.txtnewsfinal);
          for(News_data item : college_item.news_list){
              news_value=news_value+item.news+" ";
          }
          txtnewsfinal.setText(news_value);
  
  
          return v;
      }
      @Override
      public void onSaveInstanceState(Bundle outState) {
  
      }
      }
  </code></pre>
  
  <p>The LOGCAT:</p>
  
  <pre><code>07-14 19:31:25.664: E/AndroidRuntime(22537): FATAL EXCEPTION: main
  07-14 19:31:25.664: E/AndroidRuntime(22537): java.lang.RuntimeException: Unable to destroy activity {com.igloo.collegefinder/com.igloo.collegefinder.FragmentMain}: java.lang.IllegalStateException: Activity has been destroyed
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2676)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2694)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.app.ActivityThread.access$2100(ActivityThread.java:117)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.os.Handler.dispatchMessage(Handler.java:99)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.os.Looper.loop(Looper.java:130)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.app.ActivityThread.main(ActivityThread.java:3689)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at java.lang.reflect.Method.invokeNative(Native Method)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at java.lang.reflect.Method.invoke(Method.java:507)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at dalvik.system.NativeStart.main(Native Method)
  07-14 19:31:25.664: E/AndroidRuntime(22537): Caused by: java.lang.IllegalStateException: Activity has been destroyed
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1376)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at com.igloo.collegefinder.CollegeDetails.onDestroyView(CollegeDetails.java:168)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.support.v4.app.Fragment.performDestroyView(Fragment.java:1705)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1007)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1097)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1932)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:335)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2663)
  07-14 19:31:25.664: E/AndroidRuntime(22537):    ... 11 more
  </code></pre>