Program restarts when switching orientation.

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Program restarts when switching orientation.

Postby GasBot » Tue Nov 11, 2008 4:39 pm

I know that you can insert a flag in the android manifest to stop it from destroying the app, but I'm wondering if there's an easy way to just allow the app to switch orientations without restarting. I don't want to stop the orientation from changing, I just want it to do it seemlessly and not restart my app.

Anyone tackle this one before?
GasBot
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Fri Oct 17, 2008 7:19 am

Top

Postby Sweevo » Thu Nov 13, 2008 9:13 am

If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

This is done because any application resource, including layout files, can change based on any configuration value. Thus the only safe way to handle a configuration change is to re-retrieve all resources, including layouts, drawables, and strings. Because activities must already know how to save their state and re-create themselves from that state, this is a convenient way to have an activity restart itself with a new configuration.

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.

Source: http://code.google.com/android/referenc ... ivity.html
Sweevo
Developer
Developer
 
Posts: 35
Joined: Thu Oct 16, 2008 9:27 am

Postby mike_collins » Sat Nov 22, 2008 1:39 am

I have put
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ebay.mobile"
android:versionCode="1"
android:versionName="1.1"
android:configChanges="orientation|keyboardHidden"
>

in the manifest and put an override in the main activity and
a login dialog, as in

public void onConfigurationChanged(Configuration newConfig)
{
Toast.makeText(this, "onConfigurationChanged", Toast.LENGTH_SHORT).show();
}

yet when opening and closing the keyboard on the G1 the current
activity is restarted and my override never gets called, or at least
I never see the toast.

Ideas? TIA
mike
mike collins
mike_collins
Freshman
Freshman
 
Posts: 5
Joined: Thu Nov 20, 2008 1:43 am
Location: Oregon

Postby GasBot » Sat Nov 22, 2008 3:33 am

mike_collins wrote:I have put
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ebay.mobile"
android:versionCode="1"
android:versionName="1.1"
android:configChanges="orientation|keyboardHidden"
>

in the manifest and put an override in the main activity and
a login dialog, as in

public void onConfigurationChanged(Configuration newConfig)
{
Toast.makeText(this, "onConfigurationChanged", Toast.LENGTH_SHORT).show();
}

yet when opening and closing the keyboard on the G1 the current
activity is restarted and my override never gets called, or at least
I never see the toast.

Ideas? TIA
mike


In your activity, change your code to...

@Override
public void onConfigurationChanged(Configuration newConfig)
{
Toast.makeText(this, "onConfigurationChanged", Toast.LENGTH_SHORT).show();

super.onConfigurationChanged(newConfig);
}

I think that'll work, if it doesn't let me know as I was able to handle this in my code.
GasBot
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Fri Oct 17, 2008 7:19 am

Postby beaster » Wed Dec 03, 2008 10:37 am

don't work!((((((((((((
beaster
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Oct 07, 2008 10:43 am

Postby blee » Thu Mar 19, 2009 11:09 pm

I am having the same issue.

Basically my app has a log in screen. You slide the keyboard and it changes the display to match. You enter the info, after you log in, I display the next screen in landscape mode. You then slide the keyboard close, but it does not load the layout for portrait mode.

I feel that I need to edit my java code so it will redraw the screen with the right layout, but I do not know how ( I am new to java and android).

Here is my java function so far:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public void onConfigurationChanged(Configuration newConfig) {
  2.  
  3.         super.onConfigurationChanged(newConfig);
  4.  
  5.        
  6.  
  7.     }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Any help would be wonderful.

thanks.
blee
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Mar 19, 2009 11:04 pm

Top

Postby icobasco » Wed Jan 13, 2010 4:48 pm

blee wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    
  2. public void onConfigurationChanged(Configuration newConfig) {
  3.         super.onConfigurationChanged(newConfig);
  4.        
  5. }
  6.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



If you set in the manifest

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


you are saying Android that you want to be notificated for orientation and keyboard changes.
So Android will call your

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    
  2. @Override
  3. public void onConfigurationChanged(Configuration newConfig) {
  4.   super.onConfigurationChanged(newConfig);
  5.   setContentView(R.layout.my_landscape);
  6. }
  7.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


where you need to create "from zero" your activity.
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests