ListFragment onCreate called twice

大城市里の小女人 提交于 2020-01-05 08:04:39

问题


how can I avoid that the onCreate method in the ListFragment ist called twice?

I have search functions for my list. The search results might be shorter than the list, which I display after creating the ListFragment (at the moment you can see the long list in the background and the shorter list with the search result in the foreground).

Here my code snippets:

in my 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);
    }

from my 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);
        }
    }

Why are the methods called twice?

Thanks in advance for your answers,

update but it still does not work:

    @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);
    }

回答1:


Put your fragment initialization code in if-else like this:

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();
}

It will solve your problem. Make sure to move listFragment = new PlantListFragment(); line inside if-else.



来源:https://stackoverflow.com/questions/15005937/listfragment-oncreate-called-twice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!