问题
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