ListFragment的onCreate调用两次 [英] ListFragment onCreate called twice

查看:156
本文介绍了ListFragment的onCreate调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能避免在ListFragment IST onCreate方法调用了两次?

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

下面我code片段:

在我的FragmentActivity:

  @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        plantManager =新PlantManager(本);
        listFragment =新PlantListFragment();
        fragmentActivityListener = listFragment;        如果(getSupportFragmentManager()。findFragmentByTag(标签)== NULL){
            尝试{
                。getSupportFragmentManager()调用BeginTransaction()取代(R.id.fragment_plantlist,listFragment,标签).commit();
            }赶上(例外前){
                ex.printStackTrace();
                CharSequence的文字= this.getString(R.string.info_error_list);
                吐司面包= Toast.makeText(这一点,文本,Toast.LENGTH_LONG);
                toast.show();
            }
        }        //力犯
        getSupportFragmentManager()executePendingTransactions()。        buttonSearch =(按钮)findViewById(R.id.button_search);
        buttonSearch.setOnClickListener(searchListAktionen);
    }

从我ListFragment:

  @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        如果(savedInstanceState == NULL){
            Log.d(TAG的onCreate称为);
            plantManager =新PlantManager(getActivity());
            装载机= getLoaderManager()initLoader(0,null,则此)。
            myAdapter =新PlantListCustomCursorAdapter(getActivity(),myCursor);
        }
    }    / **
     * @参数savedInstanceState
     * /
    @覆盖
    公共无效onActivityCreated(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        如果(savedInstanceState == NULL){
            Log.d(TAGonActivityCreated称为);
            // ListView的LV = getListView();
            // LayoutInflater吹气= this.getLayoutInflater(savedInstanceState);
            // ViewGroup中头=(ViewGroup中)inflater.inflate(R.layout.header_aktionenlist,LV,假);
            // lv.addHeaderView(头,空,假);
            this.getListView()setAdapter(myAdapter)。
        }
    }

为什么方法调用两次?

预先感谢您的回答,

更新,但它仍然无法正常工作:

  @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        如果(savedInstanceState == NULL){
            如果(getSupportFragmentManager()。findFragmentByTag(标签)== NULL){
                的setContentView(R.layout.activity_main);
                listFragment =新PlantListFragment();
                尝试{
                    。getSupportFragmentManager()调用BeginTransaction()取代(R.id.fragment_plantlist,listFragment,标签).commit();
                    //力犯
// getSupportFragmentManager()executePendingTransactions()。
                }赶上(例外前){
                    ex.printStackTrace();
                    CharSequence的文字= this.getString(R.string.info_error_list);
                    吐司面包= Toast.makeText(这一点,文本,Toast.LENGTH_LONG);
                    toast.show();
                }
            }
        }
        fragmentActivityListener = listFragment;
        plantManager =新PlantManager(本);        buttonSearch =(按钮)findViewById(R.id.button_search);
        buttonSearch.setOnClickListener(searchListAktionen);
    }


解决方案

把你的片段初始化code在的if-else 是这样的:

 如果(savedInstanceState == NULL){
    listFragment =新PlantListFragment();
    如果(getSupportFragmentManager()。findFragmentByTag(标签)== NULL){
        尝试{
            。getSupportFragmentManager()调用BeginTransaction()取代(R.id.fragment_plantlist,listFragment,标签).commit();
        }赶上(例外前){
            ex.printStackTrace();
            CharSequence的文字= this.getString(R.string.info_error_list);
            吐司面包= Toast.makeText(这一点,文本,Toast.LENGTH_LONG);
            toast.show();
        }
    }    //你为什么犯力?
    // getSupportFragmentManager()executePendingTransactions()。
}

这将解决您的问题。确保移动 listFragment =新PlantListFragment(); 行的if-else

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

解决方案

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.

这篇关于ListFragment的onCreate调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆