title: App crash by "back" to maps fragment
body: <p>I working on my first app and got problems with the "back" function.</p>
  
  <p>When i open the app an click in the menu to another fragment (2,3 or 4) and i push the back button the app crashes. When i start the app and click <code>2 &gt; 3 &gt; 4</code> (in this order) in the menu and push the back button i go back to <code>3 &gt; 2 &gt; crash</code>. The app crashes when i go back to the mainFragment which contains a google map.</p>
  
  <p>I think my problem has something to do with the googlemap in this fragment (the fragment to navigate back to) because when i set 2 enters before the error line in the xml of that view and run again, the error line number is +2. and when i comment out this fragment the back button works fine.</p>
  
  <p>By navigate to an new fragment i call:</p>
  
  <pre><code>fragmentTransaction.replace(R.id.fr_content_container, new NewsFragment()).addToBackStack(null).commit();
  </code></pre>
  
  <p>The error i get is</p>
  
  <pre><code>04-01 14:26:39.679 19054-19054/aaeu.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                            Process: aaeu.app, PID: 19054
                                                            android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment
                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                at aaeu.app.presentationlayer.tab_AlertMapsOverview.onCreateView(mapsFragment.java:52)
                                                            .....................
  </code></pre>
  
  <p>The XML of the mapsFragment is: </p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" &gt;
  
          &lt;fragment
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_weight="2"
              android:name="com.google.android.gms.maps.SupportMapFragment"
              android:id="@+id/mapwhere" /&gt;
      &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>The code  of the map fragment is:</p>
  
  <pre><code>    private SupportMapFragment mMapFragment;
      private AlertManager mAlertManager;
      private GoogleMap mGoogleMap;
      HashMap&lt;Marker, Alert&gt; mMarkerMap;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mAlertManager = new AlertManager(getContext());
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.overview_maps_alert_tab, container, false);
          ButterKnife.bind(this, view);
  
          loadMap();
  
          return view;
      }
  
      private void loadMap() {
          mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapwhere);
          if (mMapFragment == null) {
              FragmentManager fragmentManager = getFragmentManager();
              FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
              mMapFragment = SupportMapFragment.newInstance();
              fragmentTransaction.replace(R.id.mapwhere, mMapFragment).commit();
          }
  
          if (mMapFragment != null) {
              mMapFragment.getMapAsync(new OnMapReadyCallback() {
                  @Override
                  public void onMapReady(GoogleMap googleMap) {
                      if (googleMap != null) {
                          googleMap.getUiSettings().setAllGesturesEnabled(true);
  
                          LatLng marker_latlng = new LatLng(55.042684, 14.125549);
  
                          CameraPosition cameraPosition = new CameraPosition.Builder().target(marker_latlng).zoom(3.0f).build();
                          CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
                          googleMap.moveCamera(cameraUpdate);
  
                          setGoogleMap(googleMap);
                      }
                  }
              });
          }
      }
  
      private void setGoogleMap(GoogleMap googleMap) {
          this.mGoogleMap = googleMap;
      }
  
  
      // This method is called by the asynctask who downloads the alerts
      @Override
      public void UpdateAlerts(List&lt;Alert&gt; alerts) {
  
          mMarkerMap = new HashMap&lt;Marker, Alert&gt;();
          try {
          for (Alert alert : alerts) {
              MarkerOptions marker = new MarkerOptions();
              marker.position(alert.Location.get(0));
              marker.title(alert.GetFullName());
  
              Marker m = mGoogleMap.addMarker(marker);
              mMarkerMap.put(m, alert);
          }
  
  
              mGoogleMap.setOnMarkerClickListener(
                      new GoogleMap.OnMarkerClickListener() {
                          @Override
                          public boolean onMarkerClick(Marker marker) {
                              startDetailView(marker);
                              return false;
                          }
                      }
              );
          }
          catch (NullPointerException e)
          {
              Log.i("tab_AlertMapsOverview","No missings to create markers for or set clicklisteners on. Are there missings in the configured Areas?");
          }
      }
  </code></pre>
  
  <hr>
  
  <p>Near this problem i have a little question too. When i have 3 fragments, 1, 2 and 3. When i (for example) start in activity 1 and navigate to 2 > 3 > 2 > 3 i need to push 4 times back to come back in 1. Is it posible to use a kind of  "tree" system for back navigation. so i allways go up 1 level? Maybe this clears up what i mean. </p>
  
  <p><a href="https://i.stack.imgur.com/THkOc.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/THkOc.jpg" alt="this"></a>
  I think also the current situation is far from optimal becouse the fragments are still active in RAM? or are i'am wrong?</p>