在我的活动中,我发现我需要在标签视图中为我的一个标签存储状态。这是搜索栏的结果,我需要一个活动来接收和存储数据。
虽然我的应用程序在我的活动
中实现之前就已经开始工作了public void onSaveInstanceState( Bundle outState ){
ArrayList<SearchData.SearchDataSerial> sdSerial = new ArrayList<SearchData.SearchDataSerial>();
int i;
for( i = 0; i < mSearchData.size() ; i++ ){
SearchData item = mSearchData.get(i);
SearchData.SearchDataSerial elem = new SearchData.SearchDataSerial( item );
sdSerial.add( elem );
}
outState.putParcelableArrayList( "searchterms", sdSerial );
Log.d( TAG, "OnSaveInstanceState" );
}
它打破了标签中所有其他片段的持久性。
如果在onCreate中进行活动,如果我必须将我的代码更改为此以保持页面加载。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* ... not relevant */
// if( savedInstanceState == null ) { /* had to comment out this line */
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
SlidingTabsBasicFragment fragment = new SlidingTabsBasicFragment();
transaction.replace(R.id.tabbed_layout_main, fragment);
transaction.commit();
//} /* had to comment out this line */
if (savedInstanceState != null) {
loadSavedData( savedInstanceState );
}
setupActionBar();
}
答案 0 :(得分:1)
正如docs所说:
警告:您必须始终调用超类的实现 onSaveInstanceState()因此默认实现可以保存状态 视图层次结构。
所以正确的代码看起来应该......
@Override
public void onSaveInstanceState( Bundle outState ){
super.onSaveInstanceState( outState ); /* Needs to be here for framework to work */
ArrayList<SearchData.SearchDataSerial> sdSerial = new ArrayList<SearchData.SearchDataSerial>();
int i;
for( i = 0; i < mSearchData.size() ; i++ ){
SearchData item = mSearchData.get(i);
SearchData.SearchDataSerial elem = new SearchData.SearchDataSerial( item );
sdSerial.add( elem );
}
outState.putParcelableArrayList( "searchterms", sdSerial );
Log.d( TAG, "OnSaveInstanceState" );
}