I have two fragment in my app. The first is login, the second is OTP fragment. In OTP fragment when I press onBackPressed() the value in textfield at login fragment is missing. How I can make all value in textfield at login fragment still exist?
Asked
Active
Viewed 152 times
0
-
1Have you read the documentation on [saving your fragment's state](https://developer.android.com/guide/fragments/saving-state)? Any `EditText` will retain its state automatically if it has an `android:id`. – ianhanniballake Feb 25 '22 at 04:36
-
ya, I use TextInputEditText inside TextInputLayout and I set the `android:id` – Cadis Hangoluan Feb 25 '22 at 05:03
-
Is it retain state just for `EditText`? – Cadis Hangoluan Feb 25 '22 at 05:11
-
You can bundle up the value and send it back to the login fragment, when `onBackPressed()` is called – Abhishek Dutt Feb 25 '22 at 05:54
1 Answers
0
Which methods do you use to open OTP framgent ?? Add or replace.
Maybe you are using replace so when you press back, the view of login fragment will be recreated
You may find the diff here: Difference between add(), replace(), and addToBackStack()

Trung Nguyen
- 31
- 5
-
I'm using android navigation, but before go to OTP fragment I open Dialog(context: Context) – Cadis Hangoluan Feb 25 '22 at 09:41
-
As i know Navigation use replace method. So it will recreate the view @CadisHangoluan – Trung Nguyen Feb 25 '22 at 09:57
-
You can read this post for more: https://stackoverflow.com/questions/54581071/fragments-destroyed-recreated-with-jetpacks-android-navigation-components – Trung Nguyen Feb 25 '22 at 09:58