开发者

Android screen orientation bug

I am using android HTC HERO 2.1 version.

The activity I write :

<activity android:name=".gogogo"
          android:label="@string/app_name"
          android:theme="@style/Theme.mine"
          android:screenOrientation="landscape"
          android:configChanges="orientation">

let my orientation change to landscape.

However, I figured out that every time I pressed "power" button and then come back to my activity, it always start at portrait.

I tried the game : TEETER , which was written not by me. Has th开发者_StackOverflow中文版e same problem too, any one know how to fix it??

Edit: it always start at portrait. --> I mean, if you come back from power, you have to scroll down to do everything, there is something like screen lock.

You can see that there is a "status bar" at the top of the screen and at this time, the orientation is "portrait". So after You scroll the "screen lock" a little bit down, you can see the activity (such as TEETER) is at "portrait" state. When you scroll all the way down, the activity's orientation will suddenly change to the state "landscape".

So the conclusion is : My activity is forced to orient once. And I really want to know how to fix it!


Hai Steven Shih: Pls go through this page of developer guide.

http://developer.android.com/guide/topics/manifest/activity-element.html

and try this line

 android:windowSoftInputMode="stateUnspecified|adjustPan" 


Not sure if it's the same problem, but here is the problem I had :

  • I force the orientation as landscape in the manifest
  • My activity has a two surfaceviews ( a camera preview and a layer on top of it )
  • After pressing the power button and coming back, my views were suddenly in the wrong orientation

It seems to be the same problem as another question : Disable orientation but detect orientation

I think the problem is that once you press the power button, the lock screen forces the orientation as portrait. In the onConfigurationChanged I get an orientation change right when I press the power button, which was unexpected because I told to explicitly lock the orientation. The fact is that when another activity which comes in front forces the orientation, the other activities get notified of the orientation change, even if they asked for an explicit orientation.

Here's my workaround :

  • in onPause, detach the views with removeView ( my application has programmatically created views, not an xml layout )
  • in onResume , first check if the orientation is the one you requested ( getResources().getConfiguration().orientation ). If it's the case, reattach the views ( addContentView or whatever ).
  • if the orientation is not the one you expect, call setRequestedOrientation
  • onConfigurationChanged will be called to tell the orientation has been changed to the one you requested. At this point you can reattach the views with setContentView.

Seems a bit complicated, but that's what I had to go through in my particular case, since my views are created dynamically and directly attach to the default FrameLayout of the activity. Maybe I then missed some default behaviors because of that, but this way I could achieve the reactivity I was looking for.


Not sure whether this can be called a fix, but it's kind of a work-around.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    if (hasFocus)
        setContentView(mGameView);
    else
        setContentView(mEmptyView);
}

Where mGameView is your ordinary content and mEmptyView is just a simple View object. The activity will still be drawn in portrait for a frame or two, but it will not be visible to the user.

I've tested this with HTC Hero and Nexus S.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜