在我MainActivity
,我有:片段(不一致崩潰)
@Override
protected void onResume() {
super.onResume();
checkForCrashes();
checkForTutorial();
checkForUpdates();
setStore();
setup();
}
在setup()
,我叫initializeTabs()
在回調中:
protected void setup() {
final Store s = getStore();
setBackground();
if (s == null) {
unauthorizedHandler();
return;
}
final Context mainActivity = this;
fragments = getTabFragments();
StoresController.getStoreDetails(s, mainActivity, new Callback<StoreDetailDecorator>() {
@Override
public void success(StoreDetailDecorator storeDetailDecorator, Response response) {
s.prettyName = storeDetailDecorator.store.pretty_name;
s.save();
Log.v(TAG, s.prettyName);
TextView toolbar_label = (TextView)findViewById(R.id.toolbar_label);
toolbar_label.setText(MainActivity.getTruncatedMenuName(s.name()));
SummaryTab t1 = (SummaryTab)fragments.get(0);
t1.notifier = (SummaryTabLoadingNotifier)mainActivity;
initializeTabs(s, fragments);
t1.populateReport();
}
}
}
public void initializeTabs(Store s, List<Fragment> fragments) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), getTabTitles(), fragments);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setOffscreenPageLimit(2);
pager.setAdapter(adapter);
}
最後一行崩潰了一些客戶。
java.lang.IllegalStateException: Fragment InfoTab{1bcb80f} is not currently in the FragmentManager
但不是每一次。爲什麼有時會發生這種情況與片段沒有連接的東西?我讀了一些關於在片段上檢查isAdded()
的問題,但是這是爲了檢查片段是否添加到活動中。我使用的是FragmentStatePagerAdapter
:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private CharSequence[] titles;
private List<Fragment> fragmentList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fragmentManager, CharSequence titles[], List<Fragment> fragmentList) {
super(fragmentManager);
this.titles = titles;
this.fragmentList = fragmentList;
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public CharSequence getPageTitle(int position) { return titles[position]; }
@Override
public int getCount() { return fragmentList.size(); }
@Override
public Parcelable saveState() {
return null;
}
}
難道saveState(){ return null;}
吧?據我所知,這是在那裏,以確保每次刷新視圖。
類似於你的問題好的和明確的答案在這裏> http://stackoverflow.com/a/11296700/5188159 –