views:

17687

answers:

4

In my Android application, when I rotate the device (slide out the keyboard) then my activity is restarted (onCreate is called). Now, this is probably how it's supposed to be, but I do a lot of initial setting up in the onCreate method, so I need either:
1. Put all the initial setting up in another function so it's not all lost on device rotation or
2. Make it so onCreate is not called again and the layout just adjusts or
3. Limit the app to just portrait so that onCreate is not called.

+5  A: 

What you describe is the default behavior. You have to detect and handle these events yourself by adding:

android:configChanges

to your manifest and then the changes that you want to handle. So for orientation, you would use:

android:configChanges="orientation"

and for the keyboard being opened or closed you would use:

android:configChanges="keyboardHidden"

If you want to handle both you can just separate them with the pipe command like:

android:configChanges="keyboardHidden|orientation"

This will trigger the onConfigurationChanged method in whatever Activity you call. If you override the method you can pass in the new values.

Hope this helps.

GregD
+42  A: 

Using the Application Class

Depending on what you're doing in your initialization you could consider creating a new class that extends Application and moving your initialization code into an overwridden onCreate method within that class.

public class MyApplicationClass extends Application {
  @override
  public void onCreate() {
    super.onCreate();
    // TODO Put your application initialization code here.
  }
}

The onCreate in the application class is only called when the entire application is created, so the Activity restarts on orientation / keyboard visibility changes won't trigger it.

It's good practice to expose the instance of this class as a singleton and exposing the application variables you're initializing using getters and setters.

NOTE: You'll need to specify the name of your new Application class in the manifest for it to be registered and used.

Reacting to Configuration Changes

As a further alternative, you can have your application listen for events that would cause a restart - like orientation and keyboard visibility changes -- and handle them within your Activity.

Start be adding the android:configChanges node to your Activity's manifest node

android:configChanges="keyboardHidden|orientation"

Then within the Activity override the onConfigurationChanged method and call setContentView to force the GUI layout to be re-done in the new orientation.

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.myLayout);
}
Reto Meier
Thank you - very descriptive!
Isaac Waller
Glad I could help.
Reto Meier
Better add also android:screenOrientation="portrait" to activity properties in manifest and call super.onConfigurationChanged(newConfig) too - otherwise other (or future) configChanges won't work.
tomash
I dont think the second approach works. I tried it; one Activity with a EditText. I wrote some text there, change orientation and the text was gone/reset.
Ted
When I try it, I get an Exception saying "SuperNotCalledException". It requires that the overridden onConfigurationChanged call super.onConfigurationChanged, and that will make it do the onCreate... =(
Ted
Here's hoping we see an onRotate() method in the future. Having to even worry about things like this is—frankly—frustrating.
Kelly Sutton
Just to make things crystal clear, the android:configChanges attribute needs to go inside the <activity> tag like this: <activity android:configChanges = "orientation" > </activity>
justinl
+6  A: 

Just noticed the android-developers blog post "Avoiding memory leaks" where they talk about a kind of memory leak commonly occuring when trying to keep data across context destruct/construct sequences (of which Activity is a sub-set).

Take care so you don't end up with such a leak (although the solution in the accepted answer seems to avoid this).

Henrik Gustafsson
Thank you - I have fixed a few problems.
Isaac Waller
A: 

The onCreate Method is still called even when you change the orientation of Android. So moving all the heavy functionality to this method is not going to help you

ganesh krishnan