ListFragment onCreate called twice

2019-08-01 14:58发布

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条回答
Summer. ? 凉城
2楼-- · 2019-08-01 15:47

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.

查看更多
登录 后发表回答