1

如何避免 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);
    }
4

1 回答 1

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

于 2013-02-21T15:37:52.307 回答