Configuration changed (orientation change) and destroying Activities - is this the way it's supposed to work?

I read up on how Android handles "configuration changes" - by destroying the active Activity.

I really want to know from Android Team why this is. I would appreciate an explanation on how the reasoning went, because I don't understand it. The fact that it acts in that way puts us all, as I see it, in a world of pain.

Lets assume you have a Activity which presents a number of EditText:s, checkboxes etc. If a User starts to fill that form with text/data and then changes orientation (or get a Phonecall), then all input the User made is gone. I haven't found any way to preserve state. That forces us to make extremely painful coding to not lose all data.

As I see it, you need another "non-Activity" class (or "value-holding" class perhaps) that has one field for each "form element" (EditText, checkbox etc).

For every single "form element" that exists, you then need to attach an Event like "onChanged" (or onTextChanged or something like that) that updates the corresponding field in the "value-holding" class to make sure that for every single character you type (in a EditText for example) is saved at once.

Perhaps you can use some listener (like "onDestroy" or something) and then fill the value-holding class with data.

I have also found this piece of info where they talk about using Bundle, onSaveInstanceState and onRestoreInstanceState, but that also mean that the programmer has to manually save and then later put back the values in the correct place? This approach is a bit less messier than my suggestions above, but still not very nice.

Can someone tell me that I am totally wrong and that this is not how it works and that I totally missed some vital information?


Asked by: Rafael790 | Posted: 25-01-2022






Answer 1

You should read the Application Fundamentals (specifically, Activity lifecycle). Since Activitys must be able to handle being killed at any time due to memory contraints, etc. it's just a cleaner way to handle rotations without adding too much complexity - instead of checking every resource for an alternate resource, re-structuring the layout, etc. you just save your essential data, kill the activity, re-create it, and load the data back in (if you're willing to deal with the extra complexity of managing this yourself, you can use onConfigurationChanged to handle the configuration change yourself.) This also encourages better practices - developers have to be prepared for their Activity to be killed for orientation change, which has the (good) consequence of being prepared for being killed off by memory contraints also.

Answered by: Emma496 | Posted: 26-02-2022



Answer 2

The contents of an EditText will be saved for you automatically when rotating the screen if you put an android:id attribute on it. Similarly, if you display dialogs using Activity#showDialog, then the dialogs are reshown for you after rotating.

Answered by: Roland352 | Posted: 26-02-2022



Answer 3

on why part - short answer - because you might have resources that needed to be changed as you've rotated the phone. ( Images, layout might be different, etc )

On save - you can save you stuff to bundle and read it back.

  @Override
    protected void onSaveInstanceState(Bundle outState) {

            String story_id = "123"
            outState.putString(ContentUtils.STORYID, story_id);

    }

or you can use onRetainNonConfigurationInstance () as described here http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance()

Finally if you don't have anything you want to handle during rotation - you can ignore it by putting this into your activity in manifest

android:configChanges="keyboardHidden|orientation"

In general, i would read trough article from url above couple of times, until lifecycle is crystal clear.

Answered by: Patrick318 | Posted: 26-02-2022



Answer 4

@Alex's approach above pointed me to a really, really useful solution when using fragments:

Fragments usually get recreated on configuration change. If you don't wish this to happen, use

setRetainInstance(true); in the Fragment's constructor(s)

This will cause fragments to be retained during configuration change.

http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

Answered by: Sawyer894 | Posted: 26-02-2022



Similar questions

configuration - Where to install Android SDK on Mac OS X?

Where should the Android SDK be installed on Mac OS X?


android - Change menu button using XML configuration

If I create an Options menu with menu.xml, can I change the menu buttons' standard text size, color etc? I would like to change text size, bold, and color. I would like to avoid using themes and styles for this application.


Motorola specific crash on Android when using Configuration

Since I published a large update to one of my apps I have received hundreds of crash report, all from different motorola phones. The stack trace does not pass through my app: EXCEPTION java.lang.NullPointerException at android.content.res.Configuration.updateFrom(Configuration.java:269) at android.content.res.Resources.updateConfiguration(Resources.java:1257) at android.app.ActivityThread.handleConfiguratio...


Android configuration screen when installing

I need to display a configuration screen when a user installs my application. Is this possible to implement? That screen should not appear again.


Eclipse/Android - Debug Configuration - Device?

I have set up a "Debug Configuration" to debug my Android project. However to get it to debug on a device (HTC Desire) I have to set the Configuration's "Target" to "Manual", there seems to be no way to set in the Debug Configuration that it should go to a selected Device. It does work, but each debug session I have to "Manually" choose the HTC device from the dialog. Can I set it to always to go the Device...


iphone - Smartphone Memory Configuration

Smart-phones have built in ROM and RAM separately. Also a few phones has virtual memory support too. I would like to know what these memories are basically used for. I understand that RAM is available to user processes. But why do they have a big chunk of ROM? E.g. The wiki page for Droid Incredible says 512 MB DDR RAM 1 GB ROM (748 MB free to user) - what's this free to user?


Android Configuration Change

What happens to the width and height params declared in LayoutParams on configuration change? For eg: if I have an ImageView declared with, new LinearLayout.LayoutParams(30, 40); On Configuration Change, does the width become 40 and height 30?


Different widgets one configuration activity android

Some apps clog up the select widget activity by adding different configuration activities. like the screenshot here: Multi Configuration activities Is there a way to have one icon in the select widget (like Mini Info) and one configuration activity and set different layouts for widgets there? Thank you


configuration - How do I maintain separate production settings for my Android apps?

I'm developing one of my first Android apps. I come from an ASP.NET world where it's trivial to have separate Web.config files for dev, test, and production. Does anyone have a good, automated way of doing this for Android via Eclipse?


database - Where should I store my widget's preferences on the Configuration page on Android?

^Question^ I think that it would be too complex to open a database. Does using shared preferences make my app's data size become big. Is there a temporary storage place for my data or should I delete the data in the shared preferences for that widget. Short code samples are very appreciated :) Thank you






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top