Fragment isAdded() returns false on an already added Fragment

£可爱£侵袭症+ 提交于 2019-11-30 07:07:14

问题


I have this neat function:

private void addMapFragment(){
    if(!mapFragment.isAdded()){
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.mapContainer, mapFragment);
        ft.commit();
    }
}

I'm calling addMapFragment() in my activity's onCreate(). I then have a callback from a webrequest that calls addMapMapFragment(). The isAdded() method doesn't look useful at all since I'm getting a crash saying "Fragment already added: MapFragment[...]"

Any clue?


回答1:


FragmentTransactions are committed asynchronously. Therefore, you need to call

getFragmentManager().executePendingTransactions();

before you call

Fragment.isAdded();

That way, you can make sure that everything is up to date.




回答2:


Instead, check getSupportFragmentManager.findFragmentById() and see if you are getting the expected fragment back. If now you can add and commit.



来源:https://stackoverflow.com/questions/22485899/fragment-isadded-returns-false-on-an-already-added-fragment

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