在FragmentActivity中保存了实例

时间:2017-05-01 11:42:59

标签: java android

在我的FragmentActivity中我有一个recycleView,我想在我的Activity中保存实例,因为我有一个带有View寻呼机的tabLayout看起来在图片中 问题是如何在我的代码中保存实例以回收视图 请帮帮我

This is the image of my application

这是我的代码:

   public class Menu1Fragment extends Fragment {
        recycleAdapter imp_listArrayAdapter;
        public static ArrayList<String> arrayList = new ArrayList<String>();

        public Menu1Fragment() {
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.list_item, container, false);
            //if (savedInstanceState != null) {
            //Restore the fragment's state here
            //  }
            //

            Boolean a = false;


            ArrayList<imp_list> impLists = new ArrayList<imp_list>();
            impLists.add(new imp_list("Make a Banana", "3000", R.drawable.makabananasplit, a));
            impLists.add(new imp_list("Abdeys Apple Sizzler", "2000", R.drawable.abbeys, a));
            impLists.add(new imp_list("Cheese Cake", "1000", R.drawable.cheesecake, a));
            impLists.add(new imp_list("Choise Of Ice_Cream", "1500", R.drawable.icecream, a));
            impLists.add(new imp_list("Daeth By Chocolate", "3000", R.drawable.deathbychocolate, a));
            impLists.add(new imp_list("Make a Banana", "3000", R.drawable.makabananasplit, a));
            impLists.add(new imp_list("Abdeys Apple Sizzler", "2000", R.drawable.abbeys, a));
            impLists.add(new imp_list("Cheese Cake", "1000", R.drawable.cheesecake, a));
            impLists.add(new imp_list("Choise Of Ice_Cream", "1500", R.drawable.icecream, a));
            impLists.add(new imp_list("Daeth By Chocolate", "3000", R.drawable.deathbychocolate, a));
            impLists.add(new imp_list("Make a Banana", "3000", R.drawable.makabananasplit, a));
            impLists.add(new imp_list("Abdeys Apple Sizzler", "2000", R.drawable.abbeys, a));
            impLists.add(new imp_list("Cheese Cake", "1000", R.drawable.cheesecake, a));
            impLists.add(new imp_list("Choise Of Ice_Cream", "1500", R.drawable.icecream, a));
            impLists.add(new imp_list("Daeth By Chocolate", "3000", R.drawable.deathbychocolate, a));


            imp_listArrayAdapter = new recycleAdapter(getActivity(), impLists);
            RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.res_id);
            recyclerView.setAdapter(imp_listArrayAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            arrayList = imp_listArrayAdapter.stringArrayList;


            return rootView;

        }

1 个答案:

答案 0 :(得分:0)

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
       View rootView = inflater.inflate(R.layout.list_item, container, false);
        if(savedInstanceState == null) {
            // do nothing
        } else {
            String something = savedInstanceState.getString("string1");

        }
        return rootView;
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putString("string1","something");
    }
}

这将有助于改变方向。 如果替换片段而不将其放在backstack中,则其状态(以及保存的实例状态)将与片段一起清除。