View layout doesn't refresh on orientation change in android
Asked Answered
G

2

9

I'd like my VIEW layout to be adjusted on orientation change.

My Manifest is set up with: android:configChanges="keyboardHidden|orientation" on the activity.

I have res/layout-land, res/layout-port.

I have onConfigurationChanged in my Activity (it get's called on rotation).

In my Activity I have a ViewFlipper and inside it I have LinearLayouts.

When I START the Activity in port or land mode, the View's layout is correct (according to my res/layout-???? files).

But when I'm already in the Activity and I rotate the phone, then my View layout is not changed. The screen is rotated, and the onCreate of the activity is not called, as it's supposed to be, the onConfigurationChanged is called in the Activity, but the screen is always redrawn with the LinearLayout that was loaded when the Activity started.

I have a feeling that I miss out something in the onConfigurationChanged, but don't know what. I guess I need to call some function on the ViewFlipper or on the LinearLayout that's inside. What functions should I call?

ViewFlipper flipper;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    flipper = new ViewFlipper(getApplicationContext());
    setContentView(flipper);
    initLayout();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(flipper);
}

void initLayout() {
    MyView myview = createMyView(0);
    flipper.addView(myview, 0);
    myview = createMyView(1);
    flipper.addView(myview, 1);
    myview = createMyView(1);
    flipper.addView(myview, 1);
}

I tried to add flipper = new ViewFlipper(getApplicationContext()); to onConfigurationChanged() but that made everything black after rotation. I guess I'll have to call initLayout, but that would recalculate everything (and that's exactly what I don't want to do, and that's why I'm trying to do it with onConfigurationChanged, as opposed to the android default "dropMyActivity & createANewActivity"

SOLUTION: Previously I thought I can trigger the layout to be refreshed on rotation, but it seems I have to re-create the layout with the current context (after the orientation change)

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.category_flipper);
    ViewFlipper oldFlipper = viewFlipper;
    viewFlipper = (ViewFlipper)findViewById(R.id.categoryFlipper);
    for (int i = 0; i < 3; i++) {
        final CardLayout cl = new CardLayout(this, cardData[i]);
        viewFlipper.addView(cl);
    }
    cardData[viewIndex].restorePlayer();
    viewFlipper.setDisplayedChild(viewIndex);
    oldFlipper.removeAllViews();
}
Guntar answered 15/4, 2011 at 4:9 Comment(0)
P
9

If flipper is a reference to the view that was initially installed, that's what's causing the problem. You need to pass the resource ID of the layout you want, not the old view from before the phone was rotated. The content view will then be bound to the new resource. Maybe something like this (modulo my guess at your resource identifiers):

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.flipper);
    flipper = (ViewFlipper) findViewById(R.id.flipper);
}
Proprietress answered 15/4, 2011 at 4:22 Comment(4)
Ok, this seems to be the good direction, but still have to find out how to fit it to my case I'll add some more code to the original questionGuntar
@Flöcsy - I'm looking at initLayout, which contains work you say that you don't want to repeat, but it looks like it's just creating all the views again. This is exactly what you need to do to reset the display when the screen is rotated. If you are doing expensive calculations at the same time you create the views, and these really are independent of configuration, you can capture and re-use them as described hereProprietress
Ted, i think you're right. Although createMyView is the expensive work, but I get the idea now, and probably I'm doing much more then what I'm supposed to (for example I'm parsing a zip file to find images (to show on a ImageButton) and mp3 files (to play when the button is played).Guntar
You can keep references to the images by saving them as non-configuration data. Probably the same goes for the mp3 files.Proprietress
E
0

put that in your android manifest. android:configChanges="orientation"

in that case just try this and set your layout...

    if (Intraday.this.getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_PORTRAIT){ 


    }
    else if (Intraday.this.getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_LANDSCAPE) {      


    }
Expletive answered 15/4, 2011 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.