0

我使用底部栏库制作了一个应用程序。我在用着

编译'com.roughike:bottom-bar:2.0.2'

我有 5 个这样的片段

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);
        }

    });
}

}

MainActivity.Java

public class MainActivity extends AppCompatActivity {
boolean doubleBackToExitPressedOnce = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Declare Fragment
    final EventFragment eventFragment=new EventFragment();
    final BloodFragment bloodFragment=new BloodFragment();
    final MerchantFragment merchantFragment=new MerchantFragment();
    final StoreFragment storeFragment=new StoreFragment();
    final ProfileFragment profileFragment=new ProfileFragment();

    BottomBar bottomBar=(BottomBar) findViewById(R.id.bottomBar);
    bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
        @Override
        public void onTabSelected(@IdRes int tabId) {
            if (tabId == R.id.tab_event) {
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.contentContainer, eventFragment, eventFragment.getClass().getName())
                        .commit();
            }
            else if(tabId == R.id.tab_blood){
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.contentContainer, bloodFragment, bloodFragment.getClass().getName())
                        .commit();
            }
            else if(tabId == R.id.tab_merchant){
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.contentContainer, merchantFragment, merchantFragment.getClass().getName())
                        .commit();
            }
            else if(tabId == R.id.tab_store){
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.contentContainer, storeFragment, storeFragment.getClass().getName())
                        .commit();
            }
            else if(tabId == R.id.tab_profile){
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.contentContainer, profileFragment, profileFragment.getClass().getName())
                        .commit();
            }
        }
    });
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        finish();
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please press BACK again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;
        }
    }, 2000);
}

}

我的问题是当我在第一个片段加载完成之前打开另一个片段时,我会收到错误消息。发生错误是因为当过程完成时,我在另一个片段中。所以我想让片段在我打开另一个片段时冻结,并在我再次打开片段时恢复进程。我在我的代码中实现它有问题。请帮我 。先谢谢了。

4

1 回答 1

0

解决办法是在打开新的时候保存原来的fragment状态。

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

@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
}`
于 2017-03-14T16:50:26.507 回答