gpt4 book ai didi

android - ListFragment onCreate 调用了两次

转载 作者:行者123 更新时间:2023-11-29 21:48:49 26 4
gpt4 key购买 nike

如何避免ListFragment中的onCreate方法被调用两次?

我的列表有搜索功能。搜索结果可能比我在创建 ListFragment 后显示的列表短(目前您可以在后台看到长列表,在前台看到带有搜索结果的较短列表)。

这里是我的代码 fragment :

在我的 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);
}

来 self 的 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);
}

最佳答案

将您的 fragment 初始化代码放在 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 中。

关于android - ListFragment onCreate 调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15005937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com