我面临着一个简单的问题。我在一个片段中有两个标签,并且两个标签都有Recyclerview,点击项目后片段被更改。但是当我一次调用onBackPressed方法时,我丢失了之前的两个标签数据。这是我的mainfragment代码
tabLayout = (TabLayout) view.findViewById(R.id.tablayout);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout.addTab(tabLayout.newTab().setText("Completed"));
tabLayout.addTab(tabLayout.newTab().setText("Not Over"));
adapter = new PagerAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
tabLayout.setTabTextColors(ContextCompat.getColor(getActivity(), R.color.graytextcolor),
ContextCompat.getColor(getActivity(), R.color.toolBarPink));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
// tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.setAdapter(adapter);
这是我的PagerAdapter
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment tab1 = new TabCompleteOrders();
return tab1;
case 1:
Fragment tab2 = new TabNotOver();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
这里是我的第一个Tab片段类
public class TabCompleteOrders extends Fragment {
public RecyclerView recyclerView;
public ArrayList<ProductOrdersDetails> arrayList;
public ProgressBar pb;
public Utils utils;
public ProductOrderAdapter adapter;
public String pId;
public RecyclerView.LayoutManager mLayoutManager;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab_complete_orders, container, false);
pb = (ProgressBar) view.findViewById(R.id.progressBar);
recyclerView = (RecyclerView) view.findViewById(R.id.fragment_tab_complete_orders_recylerview);
mLayoutManager = new LinearLayoutManager(getActivity());
context = getActivity();
// getBundles();
init();
return view;
}
private void init() {
utils = new Utils(getActivity());
arrayList = new ArrayList<>();
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new ProductOrderAdapter(getActivity(), arrayList, getActivity().getSupportFragmentManager(), Constant.COMPLETED);
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(),
recyclerView, new ClickListener() {
@Override
public void onClick(View view, final int position) {
WriteSharePrefrence(getActivity(), ORDERID, arrayList.get(position).getOrderId());
WriteSharePrefrence(getActivity(), ORDERSTATUS, COMPLETED);
Fragment fragment = new OrderDetail();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.addToBackStack("orderDeliveryDetails");
ft.replace(R.id.activity_main_frame_layout, fragment).commit();
}
}));
}
那么请帮助我如何才能获得以前关于BackPress的数据?