如何避免ListFragment中的onCreate方法被调用两次?
我的列表中有搜索功能。搜索结果可能比列表更短,我在创建ListFragment后显示该列表(此时您可以在后台看到长列表,而较短列表的搜索结果在前景中)。
这里是我的代码片段:
在我的FragmentActivity中:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plantManager = new PlantManager(this);
listFragment = new PlantListFragment();
fragmentActivityListener = listFragment;
if(getSupportFragmentManager().findFragmentByTag(tag) == null){
try{
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_plantlist, listFragment,tag).commit();
}catch(Exception ex){
ex.printStackTrace();
CharSequence text = this.getString(R.string.info_error_list);
Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
toast.show();
}
}
//force commit
getSupportFragmentManager().executePendingTransactions();
buttonSearch = (Button) findViewById(R.id.button_search);
buttonSearch.setOnClickListener(searchListAktionen);
}
来自我的ListFragment:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null){
Log.d(TAG, "onCreate called");
plantManager = new PlantManager(getActivity());
loader = getLoaderManager().initLoader(0, null, this);
myAdapter = new PlantListCustomCursorAdapter(getActivity(), myCursor);
}
}
/**
* @param savedInstanceState
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null){
Log.d(TAG, "onActivityCreated called");
// ListView lv = getListView();
// LayoutInflater inflater = this.getLayoutInflater(savedInstanceState);
// ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header_aktionenlist, lv, false);
// lv.addHeaderView(header, null, false);
this.getListView().setAdapter(myAdapter);
}
}
为什么方法被调用两次?
提前感谢您的回答,
更新,但仍然无效:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
if(getSupportFragmentManager().findFragmentByTag(tag) == null){
setContentView(R.layout.activity_main);
listFragment = new PlantListFragment();
try{
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_plantlist, listFragment,tag).commit();
//force commit
// getSupportFragmentManager().executePendingTransactions();
}catch(Exception ex){
ex.printStackTrace();
CharSequence text = this.getString(R.string.info_error_list);
Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
toast.show();
}
}
}
fragmentActivityListener = listFragment;
plantManager = new PlantManager(this);
buttonSearch = (Button) findViewById(R.id.button_search);
buttonSearch.setOnClickListener(searchListAktionen);
}
答案 0 :(得分:2)
将片段初始化代码放在if-else
中,如下所示:
if (savedInstanceState == null) {
listFragment = new PlantListFragment();
if(getSupportFragmentManager().findFragmentByTag(tag) == null){
try{
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_plantlist, listFragment,tag).commit();
}catch(Exception ex){
ex.printStackTrace();
CharSequence text = this.getString(R.string.info_error_list);
Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
toast.show();
}
}
//why you are force committing?
//getSupportFragmentManager().executePendingTransactions();
}
它将解决您的问题。请务必在listFragment = new PlantListFragment();
内移动if-else
行。