我基本上有一个分为两半的布局。在我的左侧,我有一些按钮可以触发右侧布局中显示的各种片段。
当我单击每个按钮时,相应的片段将加载到片段显示区域中。一些片段,例如片段 A 和片段 D,通过查询数据库或从 Internet 获取数据等来显示复杂的数据。只要应用程序正在运行,这些数据一旦加载就不会改变。我的问题是我可以将片段恢复到以前的状态并显示它。为了更清楚 - >我点击片段A按钮,在片段A类中所有计算都完成并显示,然后我点击片段B并显示片段B然后显示C。现在当我点击片段A按钮时,我只是希望它加载数据,而不是重做计算/连接/数据库查询。
正在使用的代码:
public void onClick(View v) {
switch (position) {
case 0:
newContent = new FragmentA();
break;
case 1:
newContent = new FragmentB();
break;
case 2:
newContent = new FragmentC();
break;
case 3:
newContent = new FragmentD();
break;
case 4:
newContent = new FragmentE();
break;
default: break;
}
if (newContent != null){
mContent = newContent;
switchFragment(newContent);
}
}
public void switchFragment(Fragment fragment) {
mContent = fragment;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.replacelayout, fragment)
.commit();
}
片段代码示例
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragmenta, container, false);
}
@Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
RUN ASYNTASKS TO CONNECT/QUERYDB AND DIPLAY THE DATA
}
}
不知道该怎么做 - 使用 backStack ?? onResume() ?? 因为我不确定调用 .replace 时会调用什么函数。
提前致谢