on orientation change, keep all objects, just change content view

by Wall-E » Wed, 30 Mar 2011 01:09:38 GMT

Sponsored Links
 I want to set the content view of my activity to a different resource
when user changes from portrait to landscape and vice versa.  I want
to try to avoid having the app call onCreate every time the user
changes orientation so I read that what I'm left with is using
onConfigurationChanged(...).  I can set the new content view but
unfortunately I have to re-initialize all my views.  Is there any way
to do this?

Also, currently I have the - android:configChanges="orientation" set
for my activity and am wondering what are the states and methods it
goes through when changing the orientation because I would like this
same behavior except I just want the content view to change.

If the only way/easiest way to accomplish this is for the app to re-
create the activity, how would I go about setting the content view
based on what the orientation is?  Would I do a check in the onCreate
method to see what the orientation is and then set the content view?


Re: on orientation change, keep all objects, just change content view

by TreKing » Wed, 30 Mar 2011 01:28:44 GMT

< http://developer.android.com/guide/topics/resources/index.html> ;
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices


Sponsored Links

Re: on orientation change, keep all objects, just change content view

by Wall-E » Wed, 30 Mar 2011 02:11:54 GMT

 Ok so I read that site you linked to and although it gave me some
hints, it still didn't solve my problem.

So I already have my resource file in the layout-land folder.  I guess
the clarification I need is, how does the activity know to use that
resource file?  Do I name it the same thing and in my main layout
folder?  Also when creating my resource layout, I know I have the
option in the pull down to select portrait or landscape.  What I did
was create a layout resource with the same name as in the main layout
and made my changes and now when I go to the main layout and select
the orientation to be landscape it changes to the correct resource,
great! But... now when I run it on my device and change orientation I
don't see the same changes.  Any other hints?


Re: on orientation change, keep all objects, just change content view

by Wall-E » Wed, 30 Mar 2011 02:17:35 GMT

 Ok I got it to work by removing the
android:configChanges="orientation" in the manifest file.


Re: Re: on orientation change, keep all objects, just change content view

by Mark Murphy » Wed, 30 Mar 2011 02:19:19 GMT


Yes. If you have the default version in res/layout/main.xml, a
landscape version to override the default would go in

Of course not. You specifically told Android not to perform any
changes, by using android:configChanges. If you want Android to
automatically apply the changes, by destroying and recreating your
activity, get rid of android:configChanges. Otherwise, you have to
make whatever changes you want in Java code.

Which is why you should not write your activities the way you did.
android:configChanges is to be used in fairly limited cases, and
there's nothing in either of your posts that suggests that your
activity fits one of those cases (e.g., uses a WebView, uses a

Use onSaveInstanceState() and onRetainNonConfigurationInstance() to
pass data to the new activity from the old one, after you get rid of

Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://github.com/commonsguy 
 http://commonsware.com/blog  |  http://twitter.com/commonsguy 

_The Busy Coder's Guide to *Advanced* Android Development_ Version
1.9.2 Available!


Re: on orientation change, keep all objects, just change content view

by Wall-E » Wed, 30 Mar 2011 02:31:06 GMT

 Thanks Mark, after I made the fixed I noticed that when I changed
orientation the objects were not the same so I'll give
onSaveInstanceState() and onRetainNonConfigurationInstance() a shot.


Other Threads

1. Integrating Calendar/Contacts apps into your own application

I'm working on an application that allows a user to manage different
email accounts all from one app. The application has it's own list of
contacts / PIM (Personal Information Management) data obtained from
each email service.

To display the list of contacts/PIM data I was wondering if it would
be possible to use the built-in contacts/calendar applications without
having to add the data to the actual address book of the phone. So say
for contacts when the user launches the contacts application from
within my application it would populate the lists from a content
provider I had written and not the usual users contacts, and when the
user opens the application from the home screen as normal their usual
contact list is not affected and they would not see the contacts from
my application.

I need to do this for PIM data using the calendar application as well.
Any help on this would be much appreciated.



2. How to add a new application to the cupcake source code?

These kinds of questions should be posted to android-porting, since they are
outside the bounds of the supported SDK and so require building your own
system image.

Dianne Hackborn
Android framework engineer

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


3. Inserting the 'status-bar' within ones own full-screen View

4. How many calendars do you use with the android gmail/calendar application?

5. questions about tabHost

6. Proxy SDK 1.5r1

7. Switching carriers