Reloading fragment, Edittext's text not cleared
Asked Answered
D

2

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

Descender answered 16/6, 2017 at 4:50 Comment(3)
check out this https://mcmap.net/q/169290/-edittext-settext-not-working-with-fragmentMicroampere
I am not manually clearing text. I want to clear all controls by reloading fragmentDescender
the best way is to clear them manually. Reloading the fragment is expensive.Bricole
R
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();
Retread answered 16/6, 2017 at 5:2 Comment(0)
B
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.

Bricole answered 16/6, 2017 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.