I have a preferences activity where I can change the language and the theme of my application. From there I return to the previous activity via the Back key, and I want to recreate the activity.
I've managed to do that by reinitializing the layout in onResume
and also calling onRestoreInstanceState
from there. All the views are restored properly, with checkboxes checked if needed, edittexts filled with texts I left there previously.
But I also have a button which is initially disabled, and becomes enabled only when a radiobutton is checked. The problem with it is the following: I check the radiobutton, the button beco开发者_运维技巧mes enabled. Then I go to settings, change the theme there, and return to the first activity. When I arrive there, the radiobutton is restored as checked, but the button is disabled.
So it seems that the enabled/disabled state isn't being saved into the bundle, which seems counterintuitive. And I haven't found any code in the Android source that does this, too. Am I missing something, or do I have to write my own code for that?
EDIT: BTW, state is saved only for the views which have ids. And that button does have one, I guarantee that :)
They key here is to see what is actually enabling the button and to make sure that is consistent on every access to the application.
From your statement:
But I also have a button which is initially disabled, and becomes enabled only when a radiobutton is checked.
it appears that you have a listener on the radiobutton which enables the button. From this, I gather that the button is initially disabled, meaning that you'll have to change that every time you run the activity.
Your options are as follows:
- Have the radiobutton set some sort of global preference which is checked in onResume, enabling the button if necessary
- When the radiobutton is checked, change the global settings (or perhaps the GUI settings) to one in which the button is enabled. That can stay forever until the radiobutton is unchecked again.
- Saving the enabledness in onSaveInstanceState as you mention
It is difficult to tell which is most appropriate because you haven't mentioned the context of the use case, but I would avoid sending it through in the bundle unless it is a very occasionally used activity. It makes a lot of sense to keep track of the radiobutton setting by either storing it or changing the view until further notice.
The easiest way is to enable your button in onResume if the radiobutton is checked.
Edit: PreferenceGroup can handle dependencies between Views, but it needs the Activity to be a subclass of PreferenceActivity.
精彩评论