views:

151

answers:

1

Hi,

I am trying to create an option in my code to change layouts using preferences. I already have the two layouts created in XML, but I can't figure out how to swap between the two during runtime. I would like to cause it to check during onResume() since it is called directly after returning from the Preferences screen and when starting up, however I can't figure out the code necessary. I tried just using setContentView() a second time, but it didn't seem to work. What can I do? Thanks in advance!

+1  A: 

I'm guessing you would need to save the user's preference, then have them close and restart your app, as I believe the only time you can call setContentView() is in onCreate(). You could read the layout preference as the first thing you do in onCreate() just before you call setContentView()

An option to keep from having the user restart the app might be to create a new instance of your activity once they have changed their preference, and close the current activity.

mbaird
What code would I use to create a new instance and close the old?
Looterguf
Just open a new instance of the Activity with an Intent, and do this.finish() in the activity you want to close.
mbaird
Sorry about the dumb question, but in what order would I do that: this.finish() first or the new intent first? It seems like if I called this.finish() first it would not get to the new Intent code; however, if I called the new Intent first, wouldn't it wait for that to close to return to my app, thus not calling this.finish() and letting it just run in the background?
Looterguf
I believe firing the Intent should be an asynchronous call. I would do that first, then call this.finish(). Would be pretty easy to test if that works by just throwing a couple log messages in your code.
mbaird