[英]How to restore savedInstanceState of fragment?
I was able to save outState as follows but was unable to restore when I land on this AttendanceFragment.cs second time. 我能够按如下方式保存outState,但是当我第二次登录此AttendanceFragment.cs时无法还原。
public override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
dataGotFromServer = JsonConvert.SerializeObject(dataList);
outState.PutString(KEY_OUTSTATE, dataGotFromServer);
}
I tried here to restore but could not get it 我在这里尝试还原但无法获取
public override void OnViewStateRestored(Bundle savedInstanceState)
{
base.OnViewStateRestored(savedInstanceState);
if(savedInstanceState!=null)
{
var result = savedInstanceState.GetString(KEY_OUTSTATE, dataGotFromServer);
}
}
and also I tried on CreateView(), OnActivityCreated() and On Create() but unsuccessfull to restore. 而且我尝试了CreateView(),OnActivityCreated()和Create(),但还原失败。
And my code for fragment replacement is as 我的片段替换代码是
public void ReplaceFragment(Context context, Fragment newFragment, string TAG)
{
Android.Support.V4.App.FragmentManager fragmentManager = ((FragmentActivity)context).SupportFragmentManager;
Android.Support.V4.App.FragmentTransaction ft = fragmentManager.BeginTransaction();
ft.Replace(Resource.Id.HomeFrameLayout, newFragment);
ft.AddToBackStack(TAG);
ft.Commit();
}
Edited: This is how I call this fragment 编辑:这就是我所说的片段
case (Resource.Id.nav_attendance):
var role = session.GetUserDetails().Get(SessionManagement.KEY_ROLE).ToString();
if (role=="Student")
{
Fragment attendanceTabFragment = new AttendanceTabFragment();
customFragment.ReplaceFragment(this, attendanceTabFragment,typeof(AttendanceTabFragment).Name);
}else
{
Fragment attendanceFragment = new AttendanceFragment();
customFragment.ReplaceFragment(this, attendanceFragment, typeof(AttendanceFragment).Name);
}
Any idea or sample code much appreciated. 任何想法或示例代码非常感谢。 Thank you.
谢谢。
Unless the Activity that contains the Fragment get disposed, Fragment's OnSaveInstanceState
is not going to be called. 除非处理包含Fragment的Activity,否则将不会调用Fragment的
OnSaveInstanceState
。
In a situation were you are swapping Fragments in and out, using Fragment.Arguments
is an option instead of a singleton/static var... 在一种情况下,您使用
Fragment.Arguments
是一个选项而不是单例/静态变量来交换片段进出。
re: getArguments / setArguments 回复 : getArguments / setArguments
In using arguments: 在使用参数时:
Arguments
Arguments
OnPause
override update the Arguments/Bundle with the items you need to save. OnPause
替代中,使用您需要保存的项目更新Arguments / Bundle。 OnResume
override read the Arguments/Bundle items that you need to restore. OnResume
替代中,读取您需要还原的Arguments / Bundle项目。 Example Fragment: 片段示例:
public class Fragment1 : Fragment
{
public Fragment1(System.IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer)
{
CreateArgumentBundle();
}
public Fragment1()
{
CreateArgumentBundle();
}
void CreateArgumentBundle()
{
Arguments = new Bundle();
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.Frag1, container, false);
}
public override void OnPause()
{
base.OnPause();
Arguments.PutString("someKey", "StackOverflow");
}
public override void OnResume()
{
base.OnResume();
var someKeyString = Arguments.GetString("someKey", "someDefaultString(new bundle)");
Log.Debug("SO", someKeyString);
}
}
In your ReplaceFragment
make sure that you are assigning the TAG
in the Replace
call: 在您的
ReplaceFragment
确保要在Replace
调用中分配TAG
:
public void ReplaceFragment(Context context, Fragment newFragment, string TAG)
{
SupportFragmentManager
.BeginTransaction()
.Replace(Resource.Id.fragmentContainer, newFragment, TAG)
.AddToBackStack(TAG)
.Commit();
}
Before calling your ReplaceFragment
, check to see if the Fragment exists ( FindFragmentByTag
) before creating a new one, this example just swaps two fragments in and out and only creates new ones if the manager does not contain one: 在调用
ReplaceFragment
之前,请在创建新片段之前检查片段是否存在( FindFragmentByTag
),此示例仅将两个片段交换进出,如果管理器中不包含两个片段,则仅创建新片段:
button.Click += delegate
{
toggle = !toggle;
var frag = SupportFragmentManager.FindFragmentByTag(toggle ? "frag1" : "frag2");
frag = frag ?? ( toggle ? (Fragment)new Fragment1() : (Fragment)new Fragment2() );
ReplaceFragment(this, frag, toggle ? "frag1" : "frag2");
};
Note: You will still need to handle the other Fragment lifecycle events in the case that the hosting Activity is recycled: 注意:在回收托管活动的情况下,您仍然需要处理其他Fragment生命周期事件:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.