我需要第一次从服务器提取数据。当从另一个button
按下后退fragment
时,应恢复先前的视图,但不能再次重新创建视图并再次从服务器中提取数据。
我的fragment
的一部分如下:
AttendanceFragment.cs
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
.......
if (savedInstanceState == null)
{
rootView = inflater.Inflate(Resource.Layout.AttendanceListview, container, false);
}
.........
// this is where I get data from WebServer
GetClassSection(); // I am confused here where to put this method and pull data from server
listView.ItemClick += OnListItemClick;
return rootView;
}
public override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
outState.PutString("_dataGotFromServer", dataGotFromServer);
}
从fragment
点击登陆此button
时
我用这种方式替换了fragment
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();
}
BaseAdapter 在此处初始化
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
dataList = new List<DA_ClassSectionAttendance>();
attendanceAdapter = new AttendanceListAdapter(this.Activity, dataList);
// Create your fragment here
//set whether MenuOption show/hide from toolbar
HasOptionsMenu = true;
}
和我的适配器在此设置
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
dataGotFromServer = savedInstanceState.GetString("_dataGotFromServer"); // savedInstanceState always gets null here
listView.Adapter = attendanceAdapter;
}
我的数据从服务器中提取并在ui中更新
private async void GetClassSection()
{
try
{ ...........
Activity.RunOnUiThread(async () => {
await Task.Delay(1 * 100);
attendanceAdapter.NotifyDataSetChanged();
progress.Visibility = ViewStates.Gone;
});
}
catch (Java.Lang.Exception e)
{
}
}
谢谢。
答案 0 :(得分:0)
根据this image,在片段类上尝试OnActivityCreated
。
答案 1 :(得分:0)
当从另一个片段按下后退按钮时,应恢复先前的视图,但不能再次重新创建视图并再次从服务器中提取数据。
当按下后退按钮时,如果您不希望片段恢复数据并重新创建视图,则使用FragmentTransaction.Add()
方法而不是FragmentTransaction.Replace()
将是更好的选择。
使用Replace
方法时,它将替换添加到容器中的现有片段。这与为所有当前添加的片段调用Remove(Fragment)
基本相同,这些片段使用相同的containerViewId
添加,然后Add(int, Fragment, String)
添加了相同的参数。所以当你想重用一些片段时,你必须考虑恢复数据,它会有点复杂。
但是当您使用Add
方法时,它会向活动状态添加fragment
。此fragment
也可以选择将view
(如果Fragment.onCreateView返回非null)放入activity
的容器视图中。这意味着之前的Fragment
只是隐藏,当您想要显示之前的fragment
时,您无需担心如何恢复数据。
这是我的代码:
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();
//When you want to show your AttendanceFragment
if (newFragment == null)
{
newFragment = new AttendanceFragment();
}
if (newFragment.IsAdded)
{
ft.Show(mg);
}
else
{
newFragment.Add(Resource.Id.HomeFrameLayout, newFragment);
}
if (anotherFragmentInstance != null && anotherFragmentInstance.IsAdded)
{
ft.Hide(anotherFragmentInstance);
}
ft.AddToBackStack(TAG);
ft.Commit();
}