I have a RelativeLayout with several nested LinearLayouts. By default the LinearLayouts have visiblity set to gone so none of them are visible when the activity first loads. I have two buttons that should show/hide LinearLayouts when pressed. Everything works great except when the device is reoriented and the visibility attributes are reset to the default "gone" in the xml. How do I retain the current visible state of a view during orientation change?
Edit: Final code for anyone else with the issue. Basically just add visible view tag to SharedPreferences in the void that changes visibility and check for it in OnCreate.
[Activity(Label = "My Activity", Theme="@style/TitleBar")]
public class CallManagement : Activity
{
public LinearLayout parts;
public LinearLayout status;
ISharedPreferences p;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.CallManager);
parts = (LinearLayout)FindViewById(Resource.Id.partLayout);
status = (LinearLayout)FindViewById(Resource.Id.statLayout);
p = PreferenceManager.GetDefaultSharedPreferences(this);
var visible = p.GetString("VisibleLayout", null);
if (visible != null && visible != "None")
{
RelativeLayout container = (RelativeLayout)FindViewById(Resource.Id.container);
LinearLayout current = (LinearLayout)container.FindViewWithTag(visible);
changeVisibility(current);
}
Button statusb = (Button)FindViewById(Resource.Id.changeStat);
Button partsb = (Button)FindViewById(Resource.Id.addParts);
statusb.Click += delegate
{
LinearLayout current = status;
changeVisibility(current);
};
partsb.Click += delegate
{
LinearLayout current = parts;
changeVisibility(current);
};
}
void changeVisibility(View v)
{
LinearLayout current = (LinearLayout)v;
parts.Visibility = ViewStates.Gone;
status.Visibility = ViewStates.Gone;
var editor = p.Edit();
if (v.Visibility == ViewStates.Gone)
{
v.Visibility = ViewStates.Visible;
editor.PutString("VisibleLayout", v.Tag.ToString());
}
else
{
editor.PutString("VisibleLayout", "None");
}
editor.Commit();
}
}