Reloading fragment, Edittext's text not cleared

99封情书 提交于 2019-12-12 14:57:28

问题


In my fragment there are lot of spinner and edit text and submit button is to save data, reset button is to reset all elements(Edit Texts and Spinners). I have used folllowing code to reset all controls

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();

but it doesn't clear editext. All spinners are reset but editext's text remain as it is


回答1:


detach().detach() not working after support library update 25.1.0 (may be earlier). This solution works fine after update:

note:

use runOnUiThread() to use commitNowAllowingStateLoss

    getSupportFragmentManager()
    .beginTransaction()
    .detach(oldFragment)
    .commitNowAllowingStateLoss();

   getSupportFragmentManager()
    .beginTransaction()
    .attach(oldFragment)
    .commitAllowingStateLoss();



回答2:


Try this one :

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
 ft.remove(this).replace(R.id.container, YourFragment.newInstance());;
 ft.commit();

Performance note : if you are only replacing the fragment just to reset the values then its better to reset the values manually because replacing the entire fragment involves lot of extra overhead as compared to manually resetting values.



来源:https://stackoverflow.com/questions/44581051/reloading-fragment-edittexts-text-not-cleared

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