• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps setRetainInstance(true) not always working

seafood

Lurker
I'm developing an application that consists of a Navigation Drawer Activity. I'm using the default Android Studio template for the Navigation Drawer Activity: My activity contains a Navigation Drawer Fragment. By now, the user has the choice between two fragments in the Navigation Drawer: He can either choose the Maps Fragment (that is shown by default) or a List Fragment. So my view structure looks like this:

- Activity
- Navigation Drawer Fragment
- Maps Fragment
- Google Maps Fragment​
- List Fragment
- ListView​


When the application is started, the Google Maps Fragment starts loading several tracks in an AsyncTask. This loading procedure takes up to five seconds. In order to keep the tracks loaded on orientation change, I call the setRetainInstance(true)-method on the Navigation Drawer Fragment, the Maps Fragment, the Google Maps Fragment and the List Fragment. This works fine as long as I stay in the Maps Fragment (I can even rotate the screen in the Maps Fragment). But when I change to the List Fragment and rotate the screen there, two bad things happen:
- The List (ListView) disappears, although I called setRetainInstance(true)-method in the List Fragment.
- When I go back to the Maps Fragment, the AsyncTask loading the tracks is restarted - although the tracks are already loaded.

Does somebody know why this happens?

MapFragment.java
@override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);
setRetainInstance(true);

this.mainActivity = (MainActivity)getActivity();
this.mainActivity.setMapFragment(this);

if(savedInstanceState == null) {

this.mapFragment = (MapFragment) mainActivity.getFragmentManager().findFragmentById(R.id.location_map);
googleMapView = mapFragment.getView();

mapFragment.setRetainInstance(true);
mapFragment.getMapAsync(this);

//My AsyncTask loading the tracks
trackHandler = new TrackHandler(this);
trackHandler.execute("Zuerich");

}
}

ListFragment.java
@override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);
this.setRetainInstance(true);

MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.setTopLevelStagesFragment(this);

}
 
Back
Top Bottom