freeze when switching between landscape/portrait

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

freeze when switching between landscape/portrait

Postby cosminb » Sun Dec 20, 2009 2:31 pm

Using the simplest OpenGL setup (i.e. just clear the screen), if I repeatedly switch between landscape and portrait modes, the app freezes, and then I have to force close it. I am using Android 1.5, and the freeze happens on both the 1.5 emulator and on my HTC Magic. Anyone knows what goes wrong?
cosminb
Freshman
Freshman
 
Posts: 6
Joined: Sun Dec 13, 2009 12:20 pm

Top

Postby zorro » Wed Jan 06, 2010 9:59 am

When you make portrait-landscape switches, Android destroys your activity and creates a new one for you, so if you do not handle this with care, the app might crash. To tell Android that you want to handle the orientation yourself you must add in the manifest file under activity tag the line:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:configChanges="orientation|keyboardHidden"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


This way the activity is not destroyed anymore when the orientation changes. You will be able to track the change in display orintation in the onSurfaceChanged function.

If you don't want the app to change orientation at all (even if you slide the keyboard on G1, or tilt the device) you must add the following line in onCreate (activity class):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


Hope this helps..
User avatar
zorro
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Mon Aug 10, 2009 3:11 pm
Location: Romania

Postby cosminb » Wed Jan 06, 2010 4:07 pm

Hi zorro,

Thank you for the reply.

I would like to handle it with care, instead of avoiding the problem. But I don't know what I'm doing wrong. What I did was just take their basic example, given on their developer site, which only clears the screen, and change the orientation several times. Run it without the USB cord attached, on a real device, not emulator. On my HTC Magic it freezes after ~5 switches.

Anyway, my question is: what's wrong with their example? Here's what I'm talking about:

http://developer.android.com/resources/ ... eview.html

Thanks,
Cosmin
cosminb
Freshman
Freshman
 
Posts: 6
Joined: Sun Dec 13, 2009 12:20 pm

Postby zorro » Wed Jan 06, 2010 4:40 pm

That sample is hot handling the screen rotation and this is the reason for the crash. The sample is just setting things up, but is assuming that you don't change the orientation. I tried the sample on a G1 with Android 1.6 and it freezes after a few switches, just like you said. Then I added the line
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:configChanges="orientation|keyboardHidden"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

into the manifest in the activity tag, and now works just fine. The screen is rotating fine and the application does not crash. Just try it. The line tells the OS that your application does not need to be reseted at orientation changes.
User avatar
zorro
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Mon Aug 10, 2009 3:11 pm
Location: Romania

Postby cosminb » Wed Jan 06, 2010 4:58 pm

Thanks, I'll give it a try when I get home. :)
cosminb
Freshman
Freshman
 
Posts: 6
Joined: Sun Dec 13, 2009 12:20 pm

Postby cosminb » Thu Jan 07, 2010 12:35 am

It worked, no more freezes. Thank you very much. :D

Do you happen to know how to do a proper switch to fullscreen also? :) This is the only thing left that's bugging me. In order to get fullscreen on-the-fly, I set a static bool flag in my class, then 'restart' the activity, and set up everything before calling setContentView(). This works fine on the emulator, just like the freeze issue, however on the device, it closes the activity/app, leaving only the Android homescreen. It happens on the first or second try when toggling fullscreen. One weird thing: if I start my activity fullscreen first, then deactivate fullscreen, and so on, it works. However, if I start the app in non-fullscreen mode, then I go to fullscreen, and then back, it kindof closes the app (i.e. shows home screen).
cosminb
Freshman
Freshman
 
Posts: 6
Joined: Sun Dec 13, 2009 12:20 pm

Top

Postby zorro » Thu Jan 07, 2010 9:39 am

You can switch to fullscreen in two ways, from the app or using the manifest file.

1. In activity onCreate function:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. protected void onCreate(Bundle savedInstanceState)
  3.  
  4. {
  5.  
  6.         super.onCreate(savedInstanceState);
  7.  
  8.  
  9.  
  10.         // set fullscreen
  11.  
  12.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  13.  
  14.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  15.  
  16.                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
  17.  
  18.         (...)
  19.  
  20.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


OR

2. In manifest file:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.  
  3. <application android:icon="@drawable/icon" android:label="@string/app_name"
  4.  
  5. android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
  6.  
  7.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


That's it.. Both methods work just fine. Second method is better I guess because the app starts fullscreen. The first method sets the fullscreen after the window appears (and also someone here on the forum said that the performance is lower too). I didn't notice performance differences between the two methods, but I reccomend the second method (which i'm also using right now).
User avatar
zorro
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Mon Aug 10, 2009 3:11 pm
Location: Romania

Postby Shaka » Thu Jan 07, 2010 2:44 pm

zorro wrote:You can switch to fullscreen in two ways, from the app or using the manifest file.

1. In activity onCreate function:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. protected void onCreate(Bundle savedInstanceState)
  2. {
  3.         super.onCreate(savedInstanceState);
  4.  
  5.         // set fullscreen
  6.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  7.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  8.                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
  9.         (...)
  10.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


OR

2. In manifest file:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <application android:icon="@drawable/icon" android:label="@string/app_name"
  3. android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
  4.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


That's it.. Both methods work just fine. Second method is better I guess because the app starts fullscreen. The first method sets the fullscreen after the window appears (and also someone here on the forum said that the performance is lower too). I didn't notice performance differences between the two methods, but I reccomend the second method (which i'm also using right now).


Amen to that. For some reason my game slows to a crawl when changing to fullscreen through code. i don't know if any of you have encountered the same problem but it's really annoying since I want it to be a part of my engine's initialization code.
Shaka
Developer
Developer
 
Posts: 44
Joined: Fri Aug 28, 2009 3:01 pm

Postby DanXS » Sun Jan 17, 2010 1:43 pm

Thanks Zorro,

I'm not the one that asked about this, but was having the same problem. Works fine now. I guess the orientation flag is the most important, perhaps people might want the keyboard, maybe the keyboard is the thing that is crashing it? Who knows anyway, simple solution that means my apps work fine now. I had assumed it was something I wasn't doing right with synchronization. But no, it is a little flag that every android developer must know about if they don't want applications to crash for apparently no reason.

Thanks a lot for that :)

zorro wrote:That sample is hot handling the screen rotation and this is the reason for the crash. The sample is just setting things up, but is assuming that you don't change the orientation. I tried the sample on a G1 with Android 1.6 and it freezes after a few switches, just like you said. Then I added the line
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:configChanges="orientation|keyboardHidden"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

into the manifest in the activity tag, and now works just fine. The screen is rotating fine and the application does not crash. Just try it. The line tells the OS that your application does not need to be reseted at orientation changes.
DanXS
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Jan 17, 2010 1:33 pm
Location: UK

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests