4

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

MRX
  • 1,400
  • 3
  • 12
  • 32
Rajesh N
  • 6,198
  • 2
  • 47
  • 58

2 Answers2

0

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();
altu
  • 357
  • 2
  • 13
0

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.

Saurabh Padwekar
  • 3,888
  • 1
  • 31
  • 37