丢失片段更改数据

时间:2018-04-25 11:16:14

标签: android fragment tabview

你能帮我解决这个问题。

我有一个带有底部导航栏的Activity,可以更改中间的片段。其中一个选项是使用选项卡显示分为2组的itens列表。 在我第一次打开片段时,所有数据都按预期存在,但是我更改片段并再次使用列表调用片段的newInstance,所有数据都消失了。请参阅下面的GIF以便更好地理解:

enter image description here

以下是设置标签的代码:

 private void setViewPager(){
        List<Fragment> frag = new ArrayList<>();
        frag.add(MyReservationsFragment.newInstance(true));
        frag.add(MyReservationsFragment.newInstance(false));

        MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter(getFragmentManager(), frag, getContext());
        vpaReservations.setAdapter(myViewPagerAdapter);
        tabReservations.addTab(tabReservations.newTab().setText(getString(R.string.s_tab_1)));
        tabReservations.addTab(tabReservations.newTab().setText(getString(R.string.s_tab_2)));
        tabReservations.setupWithViewPager(vpaReservations);
    }

以下是我实例化每个片段的方法:

public class MyReservationsFragment extends Fragment implements MyReservationsContract.View{

    private static final String TAG = "MyResFrag";
    private static final String ONLY_PENDING = "onlyPending";
    private static final String RESERVATION_ID = "reservationId";

    @BindView(R.id.rvi_my_reservations) protected RecyclerView rviMyReservations;

    private boolean onlyPending;
    private MyReservationsContract.Presenter mPresenter;


    public MyReservationsFragment() {
        // Required empty public constructor
    }

    public static MyReservationsFragment newInstance(boolean onlyPending) {
        MyReservationsFragment frag = new MyReservationsFragment();
        Bundle args = new Bundle();
        args.putBoolean(ONLY_PENDING, onlyPending);
        frag.setArguments(args);

        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            onlyPending = getArguments().getBoolean(ONLY_PENDING);
        }

        mPresenter=new MyReservationsPresenter();

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_reservations_list, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        List<MyReservation> myReservations = Const.getMyReservations(onlyPending);

        List<Reservation> myReservations = mPresenter.getUserReservationsByStatus(onlyPending);
        setRecyclerView(myReservations);
    }

    private void setRecyclerView(final List<Reservation> myReservations){
        rviMyReservations.setHasFixedSize(true);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        rviMyReservations.setLayoutManager(mLayoutManager);

        MyReservationAdapter mAdapter = new MyReservationAdapter(myReservations,getContext());
        mAdapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = rviMyReservations.getChildLayoutPosition(view);
                Intent intent = new Intent(getActivity(), ReservationDetailActivity.class);
                intent.putExtra(RESERVATION_ID, myReservations.get(position).getId());
                startActivity(intent);
            }
        });

        rviMyReservations.setAdapter(mAdapter);
        rviMyReservations.setItemAnimator(new DefaultItemAnimator());
    }


}

我已经尝试保存“onlyPending”变量的标志,但由于片段没有被破坏,它具有相同的行为。我已经坚持了2天了,你们可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

每次都不要使用new运算符创建实例,将创建片段的新对象,旧数据将消失。所以像这样创造,

首先在片段类中:

  public static  MyReservationsFragment frag;
newInstance方法中的

public static MyReservationsFragment newInstance(boolean onlyPending) {
    if(frag == null)
    {
         frag = new MyReservationsFragment();
    }

    Bundle args = new Bundle();
    args.putBoolean(ONLY_PENDING, onlyPending);
    frag.setArguments(args);

    return frag;
}