Why is onCreate() called when handset orientation changes?

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

Why is onCreate() called when handset orientation changes?

Postby steff@MA » Tue Dec 15, 2009 8:37 am

Hi everyone,

I got my hands on Android which is really fun. But at some point I
don't seem to understand the concept. I've got a Main class which
reads like this:

public class Main extends Activity implements SensorEventListener
{ ... }

in the onCreate(...) I set the layout to
setContentView(R.layout.main);
and add an OnTouchEvent Listener to the sole View in main.xml:
myView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {...

As you can see I also implemented a SensorEventListener which is used
if required. All works fine so far.

But my problem is this (at this point my misunderstanding kicks in):
whenever I rotate the device the onCreate(), onStart() etc. methods
are called, causing my app to act as if it just started. Furthermore,
I feel unable to implement an onSizeChanged(int w, int h, int oldw,
int oldh) { ... } Listener.

Can anyone please explain where my error in reasoning is? I am working
on a tiny app which is more or less done, except for the just
mentioned bug(s). Maybe I got it all wrong but it does what it is
supposed to do (i.e. sending touch positions and accelerometer data
over the network via UDP).

Thanks for any help.
Regards,
Steff
steff@MA
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Nov 27, 2009 5:14 pm

Top

Postby wiederke » Tue Dec 15, 2009 1:37 pm

Hi,

when the orientation changes. Android destroys the activity and creates a new one. This is the reason why onCreate is executed on orientation change.


Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby steff@MA » Tue Dec 15, 2009 1:44 pm

Hi and thanks for your answer. Is there a way to avoid this behaviour? I don't want my app to restart when the orientation is changed. It should just rotate the only existing View and update the variables which store the x- and y-resolution.

Thanks in advance,
steff
steff@MA
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Nov 27, 2009 5:14 pm

Postby wiederke » Tue Dec 15, 2009 1:56 pm

Hi,

you can fix your application to an orientation setting the attribute orientation of the activity in the AndroidManifest to a orientation. The other way is to store data at shutdown and restore it at startup. Read this to get more knowledge about this topic:

http://developer.android.com/reference/ ... .Bundle%29
http://developer.android.com/reference/ ... .Bundle%29


Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

SOLVED

Postby steff@MA » Tue Dec 15, 2009 3:50 pm

Thanks for helping out. It figured it out, referring to the exact same site you pointed me to.
I just added a
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:configChanges="orientation"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
to my main Activity in the Android Manifest and overrode the onConfigurationChanged(Configuration newConfig) in the corresponding Activity like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. public void onConfigurationChanged(Configuration newConfig) {
  4.  
  5.         // TODO Auto-generated method stub
  6.  
  7.         super.onConfigurationChanged(newConfig);
  8.  
  9. }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

Works neatly.
Awesome
steff@MA
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Nov 27, 2009 5:14 pm

Postby vikikivi » Mon Feb 15, 2010 8:48 pm

I was showing a welcome screen onCreate but was popping up whenever the hidden qwerty keyboard slided out. So, theres one other config parameters that you might want to give in manifest xml for phones like droid, g1, etc.

Typically, android:configChanges="keyboardHidden|keyboard|orientation" should solve most of the changes.
vikikivi
Freshman
Freshman
 
Posts: 7
Joined: Fri Jan 22, 2010 1:19 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests