带有webview savedInstanceState的Android Fragment选项卡在方向更改时始终为null

时间:2012-12-21 16:44:53

标签: c# android-fragments xamarin.android actionbarsherlock

经过多天的搜索和尝试不同的选择后,我仍然没有找到答案。

使用ActionBarSherlock我有一个实现ActionBar.ITabListener的基本SherlockFragmentActivity

片段包含简单的webview。在配置更改期间(屏幕方向更改)savedInstanceState的onCreateView参数始终为null,因此我永远无法恢复状态。

我已尽可能简单地希望获得更多帮助。所以名字很傻。

TabFragWebActivity

public class TabFragWebActivity : SherlockFragmentActivity, Com.Actionbarsherlock.App.ActionBar.ITabListener
{

    protected override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);
        SetContentView (Resource.Layout.fragment_tabs);
        LoadTabs();
    }

    public void LoadTabs ()
    {
        this.RunOnUiThread (() => {
            SupportActionBar.NavigationMode = Com.Actionbarsherlock.App.ActionBar.NavigationModeTabs;

            var tab = SupportActionBar.NewTab ();
            tab.SetContentDescription ("desc 1");
            tab.SetText ("tab 1");
            tab.SetTag ("tab1id");
            tab.SetTabListener (this);

            SupportActionBar.AddTab (tab);  


            tab = SupportActionBar.NewTab ();
            tab.SetContentDescription ("desc 2");
            tab.SetText ("tab 2");
            tab.SetTag ("tab2id");
            tab.SetTabListener (this);

            SupportActionBar.AddTab (tab);  

        });
    }

    #region ITabListener implementation

    public void OnTabReselected (Com.Actionbarsherlock.App.ActionBar.Tab tab, Android.Support.V4.App.FragmentTransaction transAction)
    {
        //throw new NotImplementedException ();
    }

    public void OnTabSelected (Com.Actionbarsherlock.App.ActionBar.Tab tab, Android.Support.V4.App.FragmentTransaction transAction)
    {
        string tag = tab.Tag.ToString();

        var fragment = SupportFragmentManager.FindFragmentByTag (tag);
        if (fragment != null) {
            transAction.Show(fragment);
            return;
        }

        fragment = new WebFragment("http://www.google.com");
        transAction.Add(Resource.Id.realtabcontent, fragment, tag);
    }

    public void OnTabUnselected (Com.Actionbarsherlock.App.ActionBar.Tab tab, Android.Support.V4.App.FragmentTransaction transAction)
    {
        string tag = tab.Tag.ToString();

        HideFragment(transAction, tag);
    }

    public void HideFragment (Android.Support.V4.App.FragmentTransaction transAction, string tag)
    {
        Android.Support.V4.App.Fragment fragment;
        fragment = SupportFragmentManager.FindFragmentByTag (tag);
        if (fragment != null) {
            transAction.Hide (fragment);
        }
    }

    #endregion

}

WebFragment

    public class WebFragment : SherlockFragment
{
    string _webURL;
    WebView _webview;
    Activity _activity;

    public WebFragment (string webURL)
    {
        _webURL = webURL;
    }

    public override void OnAttach (Android.App.Activity activity)
    {
        _activity = activity;
        base.OnAttach (activity);
    }

    public override void OnActivityCreated (Bundle savedInstanceState)
    {
        base.OnActivityCreated (savedInstanceState);
        RetainInstance = true;
        if (_webview == null)
            initUI();
    }

    public void initUI ()
    {
        _webview = (WebView)_activity.FindViewById (Resource.Id.webView);   
        if (_webview != null) {
            _webview.SetWebChromeClient (new ROSWebChromeClient ());
            _webview.SetWebViewClient (new ROSWebViewClient ());
            _webview.Settings.JavaScriptEnabled = true;
            _webview.LoadUrl (_webURL);
        }
    }

    public override Android.Views.View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var mainView = (View)inflater.Inflate (Resource.Layout.fragment_web, container, false);

        //the savedInstanceState is always null
        if (_webview != null) {
            _webview.RestoreState (savedInstanceState);
        }

        return mainView;
    }
}

我希望我做的事情很简单。但我只是不知道它是什么。

1 个答案:

答案 0 :(得分:-1)

我已经应用了Jake上面概述的更改并编译了一个新的支持库,直到Google发布他的修复程序。

http://ara.sh/android/android-support-v4.jar

将此jar替换为您的项目以及操作栏sherlock库项目