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. How do you start a service on installation?

I need a service to begin the moment my application is installed.  Is
this possible?

My current approach is to create a BroadcastService which receives the
PACKAGE_ADDED action. However, this only seems to work after my
application is installed, not when my application is installed.

<receiver android:name=".broadcast.InstallBroadcastReceiver">
        <action android:name="android.intent.action.PACKAGE_ADDED"></action>
        <data android:scheme="package"/>


2. RuntimeException when launching activity on emulator

I'm developing on Windows with Eclipse (Ganymede) with SDK 1.0r2.  For
a few days, I was able to run and debug my program on both the
emulator and my phone.  Starting yesterday, I've been unable to launch
the activity of the program I'm developing on the emulator or phone.
(I can still run my hello world app.)  My run and debug configurations
set to launch my activity com.android.bounce.Bounce.  When I try
debugging, I get this error and stack dump:

Bounce config [Android Application]
                Thread [<3> main] (Suspended (exception
line: 2140
line: 2156
$ActivityRecord) line: 112
                        ActivityThread$H.handleMessage(Message) line:
(Message) line: 88
                        Looper.loop() line: 123
                        ActivityThread.main(String[]) line: 3742
                        Method.invokeNative(Object, Object[], Class,
Class[], Class, int,
boolean) line: not available [native method]
                        Method.invoke(Object, Object...) line: 515
                        ZygoteInit$MethodAndArgsCaller.run() line: 739
                        ZygoteInit.main(String[]) line: 497
                        NativeStart.main(String[]) line: not available
[native method]
                Thread [<13> Binder Thread #2] (Running)
                Thread [<11> Binder Thread #1] (Running)

I have tried restarting Eclipse and rebooting my computer, without

Here is the code for the activity class, which has been unchanged
since before I began getting this error:

public class Bounce extends Activity {
        private BounceThread mBounceThread;
        private BounceView mBounceView;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        // get handles
        mBounceView = (BounceView)findViewById(R.id.bounce);
        mBounceThread = mBounceView.getThread();


Stepping through in the de{*filter*}, savedInstanceState == NULL, and the
failure occurs in setContentView().  The file res/layout/main.xml is:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android=" http://www.***.com/ 



The automatically created R.java file includes:

public final class R {
    public static final class id {
        public static final int bounce=0x7f050000;

Here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" http://www.***.com/ ;
    <application android:debuggable="true" android:icon="@drawable/
icon" android:label="@string/app_name">
        <activity android:name="Bounce"
                <action android:name="android.intent.action.MAIN" />
android:name="android.intent.category.LAUNCHER" />

I'd be grateful for any advice.



3. How to remove Gallery drop shadow

4. getFromLocationName() broken?

5. Source for android.widget.TimePicker

6. Android Dev Phone Battery life

7. CPU asleep when using the network