문제

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();
    }

}
도움이 되었습니까?

해결책

The way to do this is to maintain the view's state in a field, if you have a lot of settings to maintain, you probably want to make a private class. then:

  1. override the onRetainNonConfigurationInstance method and return that instance.
  2. in the onCreate method, retrieve the instance, and if it's not null, use it to reinstate those values. something like this.activityState = (UserAccountData) getLastNonConfigurationInstance();

Most people don't realize that the activity is recreated whenever you change orientation. So this lets you communicate these settings to the new instance.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top