2

I'm using flow library(https://github.com/square/flow) to display screens in my app. I have a screen with map view, I implemented it like this:

public class MainView extends LinearLayout implements OnMapReadyCallback {

private MapView mMapView;
private MainScreen mScreen;

public MainView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOrientation(VERTICAL);

}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    mScreen = Flow.getKey(this);

    mMapView = (MapView)findViewById(R.id.map);

    mMapView.onCreate(mScreen.getGoogleMapState());
    mMapView.getMapAsync(this);
    mMapView.onResume();
}


@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    sInstance = null;
    MainScreen screen = Flow.getKey(this);

    if(mMapView != null) {
        mMapView.onPause();
        mMapView.onSaveInstanceState(mScreen.getGoogleMapState());
        mMapView.onDestroy();
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mScreen.setMapReady(true);
    }
}

My MainScreen class is saving map state:

  public class MainScreen implements Parcelable {

private Bundle googleMapState = new Bundle();
private boolean mMapReady;

public MainScreen(Context context) {
    Flow.get(context).setHistory(History.emptyBuilder().push(this).build(), Direction.REPLACE);
}

public Bundle getGoogleMapState() {
    return googleMapState;
}

public void setGoogleMapState(Bundle googleMapState) {
    this.googleMapState = googleMapState;
}

public void setMapReady(boolean mapReady) {
    mMapReady = mapReady;
}

public boolean isMapReady() {
    return mMapReady;
}

protected MainScreen(Parcel in) {
    googleMapState = in.readBundle();
    mMapReady = in.readInt() == 1;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeBundle(googleMapState);
    dest.writeInt(mMapReady ? 1 : 0);
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<MainScreen> CREATOR = new Parcelable.Creator<MainScreen>() {
    @Override
    public MainScreen createFromParcel(Parcel in) {
        return new MainScreen(in);
    }

    @Override
    public MainScreen[] newArray(int size) {
        return new MainScreen[size];
    }
};
}

But the google map view is recreated every time app is resumed. What I am doing wrong? I want that google map would be resumed just the way it was before leaving app.

sider
  • 687
  • 2
  • 9
  • 23

0 Answers0