• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Save dynamically created layout on screen rotation

jeff357

Lurker
I am creating a layout dynamically when the activity first starts, the problem is when the screen is rotated it is hitting the database again to create the layout. How can I save the layout so it isn't recreated each time the screen orientation is changed?
 
device rotation will always destroy and re-create the layout. Not sure what you mean by 'saving layout'? Do you want to save certain state's i.e buttons, listeners etc.? If the answer if yes then you can use onSaveInstanceState and onRestoreInstanceState to save and restore some states that you don't want to loose when orientation changes.
 
My app loads up checkboxes and EditText based on what is in a database. So I don't want to reload ever time a screen rotation happens. How do you save Views to a bundle?
 
My app loads up checkboxes and EditText based on what is in a database. So I don't want to reload ever time a screen rotation happens. How do you save Views to a bundle?

So the answer would be you can't save the layout, the layout has to effectively adjust to new orientation i.e. it is not an image that can just be rotated, each component has to be placed again since its x,y has changed due to rotation. I mean possibly you can if you go thru the entire tree, but that would be worthless. You can save your DB contents, decision tree etc. or keep the database outside the activity in a generic class so that cursor is not destroyed. Link below gives an example of save and restore:

How do I save an Android application's state? - Stack Overflow

Another thread of interest for you maybe

android - Restoring state of TextView after screen rotation? - Stack Overflow

Another alternative is to lock your app to vertical layout. You do that in the manifest

<activity
...
android:screenOrientation="portrait"
</activity>
 
Got it! Created a fragment and load an ArrayList of the views that are getting dynamically created during onCreate. Set setRetainInstance(true); and onPause clear the views and onResume reload the views from the ArrayList. With setRetainInstance(true), onCreate will not be called again so it will not load from the DB.
 
You dont have to let the system destroy the activity -- but that is the default behavior.

in your manifest you can specify that you want to handle orientation changes yourself by adding the XML parameter android:configChanges="orientation"

Code:
<activity 
    android:name=".activities.MyActivity"
    android:label="@string/app_name"
    [B]android:configChanges="orientation[/B]">
</activity>
Then in your activity you can handle the change with the onConfigChanged callback

Code:
@Override
public void onConfigurationChanged(Configuration newConfig) 
{
      //must call super  
      super.onConfigurationChanged(newConfig);

      // if you want to use a new layout, 
      // call this to reset the content view
      setContentView(R.layout.home_layout); 

      //set your references/values/listeners/whatever
      setReferences(); 
     
}
What wubzy said is also correct, this is just another way to go about it. :)

Using wubbzy's method can also be good in that it will help you understand how Android saves instance data from a component (like an Activity or Fragment).

If you are just starting out though, sometimes it can be easier to handle the config changes yourself (like my example), or force portrait mode (wubbzy's second example).
 
Back
Top Bottom