Garro88
Garro88

Reputation: 360

Reload a View on Orientation Change

In my Activity I have a View that "depends" from screen orientation. If it's in landscape mode, i use a layout under layout-large-hand, but if it's in portrait mode, I use the layouts under the layouts folder. The Activity shows also a Map with some markers and informations.

In my AndroidManifest i have

android:configChanges="orientation|screenSize"

But the activity shows only the layout on portrait mode.

How can I fix this?

EDIT 3/12:

I have a layout like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

<include
    android:id="@+id/my_view"
    layout="@layout/my_view"
    android:visibility="gone" />

</RelativeLayout>

and i want that my_view changes the layout, but map remains with all markers, zoom level, position, ecc ecc...

my_view is visible when i click on a Button created dinamically in the activity.

EDIT 2:

Like SweetWisher ツ says, i was trying to setup a custom behaviour for the views. But when i rotate the device, the map disappear. This is part of my code in the activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initUI();
}

private void initUI() {
    setContentView(R.layout.my_layout);

    if (mp == null) {
        mp = MapFragment.newInstance();
    }
    getFragmentManager().beginTransaction().replace(R.id.placeholder, mp).commit();

    initUIStuff()
}

private void initUIStuff(){

}



@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    initUI();

}

@Override
protected void onStart() {
    super.onStart();
    initGMap();
}

@Override
protected void onResume() {

    super.onResume();
    initGMap();
}

private void initGMap() {
    if (mp != null  {
        //Initialize Map
    }
}

Upvotes: 0

Views: 3519

Answers (1)

michal.z
michal.z

Reputation: 2075

If you use android:configChanges="orientation" in your Manifest you're preventing Android from doing default reset of view hierarchy on screen orientation change. You have to manually change your views in onConfigurationChanged method and by that I mean inflate your desired layout and replace your old layout with it. If you want to take advantage of Android automatic view hierarchy reset don't use android:configChanges="orientation".

Edit: Use following code to add map fragment through XML:

<fragment
  android:id="@+id/map"
  android:name="com.google.android.gms.maps.MapFragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

You also don't need now 'onConfigurationChange' method so delete it.

Upvotes: 0

Related Questions