i使用Bottombar库制作应用程序。我正在使用
compile '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);
}
}
我的问题是,当我在第一个片段加载完成之前打开另一个片段时,我会遇到错误。发生错误是因为过程完成后,我处于另一个片段。因此,当我打开另一个碎片并再次打开片段时,我想使片段冻结。如何在我的代码中实现它?
解决方案是在打开新的片段时保存原始片段状态。
保存可以使用的片段状态:
@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
}`