我正在尝试将数据保存在Fragment的onSaveInstanceState()中,但从不调用该方法.
有人可以帮忙吗?
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ScrollView content = (ScrollView) inflater.inflate(R.layout.content, container, false);
// More stuff
return content;
}
@Override
public void onSaveInstanceState(Bundle icicle) {
// NEVER CALLED
super.onSaveInstanceState(icicle);
//More stuff
}
}
Run Code Online (Sandbox Code Playgroud) 我正在用另一个片段替换一个片段:
FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
transaction.replace(R.id.main_container, nextFragment, nextFragment.getClass().toString());
transaction.addToBackStack(nextFragment.getClass().toString());
transaction.commit();
Run Code Online (Sandbox Code Playgroud)
然而,被替换的片段,它的 onSaveInstanceState 没有被调用。我究竟做错了什么?
我目前正在使用a custom list adapter并在运行时修改listview的行(更改文本,按钮等).
我想找到一种方法来保存/恢复listview在运行时创建的更改,当我启动另一个片段然后返回到包含listview的片段时.
目前,每次都会重新加载列表视图,并且所有运行时更改都将丢失.
以下显示了我如何设置列表适配器.
public void setAdapterToListView(ArrayList<item> list) {
ItemList = list;
ItemAdapter adapter = new MenuItemAdapter(list, getActivity(), this);
ItemListView.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)
然后我使用自定义列表适配器在运行时进行更改(如上所述).以下是我在运行时如何更改内容的片段:
if (holder.qty.getText().toString().equals("Qty")) {
relativeLayout.setBackgroundColor(row.getResources().getColor(R.color.navDrawerL ightBlue));
holder.qty.setText("1");
holder.qty.startAnimation(anim);
holder.removeItem.setBackgroundResource(R.drawable.remove_item_red);
holder.removeItem.setEnabled(true);
...
Run Code Online (Sandbox Code Playgroud)
有没有推荐的方法来解决这个问题?