打开另一个碎片时如何冻结碎片

时间:2017-03-14 16:38:52

标签: android android-fragments fragment android-bottomnav bottombar

我使用底栏库制作应用程序。我正在使用

  

compile' com.roughike:bottom-bar:2.0.2'

我有5个像这样的片段

$.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            console.log(fullNumber);
            $.ajax({
                url: url,
                data: JSON.stringify(data),
                type: 'POST',
                datatype: 'JSON',
                contentType: 'application/json',
                success: function (response) {
                    if (response.status === true) {
                        console.log(response.message);
                        $('#myModalCallback').modal('toggle');
                    } else {
                        alert('Issue');
                    }
                },
                error: function (response) {
                    $('#errormessage').html(response.message);
                }
            });

}

MainActivity.Java

public class EventFragment extends Fragment {
private RecyclerView rcEvent;
private EventAdapter mAdapter;
private List<EventModel> mgridData;
private LinearLayoutManager layoutManager;
private SwipeRefreshLayout mSwipe;


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView= inflater.inflate(R.layout.fragment_event, container, false);

    mSwipe= (SwipeRefreshLayout) rootView.findViewById(R.id.swipeRefresh);

    rcEvent = (RecyclerView) rootView.findViewById(R.id.rv);
    layoutManager = new LinearLayoutManager(getActivity());
    rcEvent.setLayoutManager(layoutManager);
    mgridData = new ArrayList<>();
    mAdapter = new EventAdapter(getActivity(), mgridData);
    rcEvent.setAdapter(mAdapter);

    fetchData();

    mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            fetchData();
            //mSwipe.setRefreshing(false);
        }
    });

    return rootView;
}

public void fetchData()
{

    mSwipe.setRefreshing(true);

    apiData api= ApiClient.getClient().create(apiData.class);
    Call<EventResponse> call=api.GetEvent();
    call.enqueue(new Callback<EventResponse>() {
        @Override
        public void onResponse(Call<EventResponse> call, Response<EventResponse> response) {
            if(response.isSuccessful())
            {
                EventResponse result=response.body();
                Log.d("Event", "response = " + new Gson().toJson(result));
                if(result !=null)
                {
                    List<EventModel> eventResponse= result.getEventData();
                    if(eventResponse !=null)
                    {
                        for (EventModel eventRespon :eventResponse) {
                            mgridData.add(eventRespon);
                        }
                        RecyclerView.Adapter adapter = new EventAdapter(getActivity(), mgridData);
                        mAdapter.setListData(mgridData);
                        rcEvent.setAdapter(adapter);
                    }
                    mSwipe.setRefreshing(false);
                }
            }
        }

        @Override
        public void onFailure(Call<EventResponse> call, Throwable t) {
            Toast.makeText((MainActivity)getActivity(), "Koneksi Ke Internet Gagal", Toast.LENGTH_SHORT).show();
            mSwipe.setRefreshing(false);
        }

    });
}

}

我的问题是当我在第一个片段加载完成之前打开另一个片段时,我会收到错误。发生错误是因为当流程完成时,我会在另一个片段中。因此,当我打开另一个片段时,我希望片段冻结,并在我再次打开片段时恢复该过程。我在我的代码中实现它有问题。请帮我 。谢谢你提前。

1 个答案:

答案 0 :(得分:0)

解决方案是在打开新片段时保存原始片段状态。

要保存片段状态,您可以使用:

@Override  
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  ...
  if (savedInstanceState != null) {
    //Restore the fragment's state here
  }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

   //Save the fragment's state here
}`