2

Although the concept is very simple, I seem to be having difficulty saving the text value of a TextEdit when the orientation of the device changes. When I change the orientation, the text inside my EditText is erased.

The view hierarchy is as follows:

Activity
    View Pager
        Fragment
            Recycler View
                View Holder
                    Edit Text
  • I tried setting freezesText to true in the xml layout for the EditText to no avail.

  • In the fragment, I tried saving the text in onSaveInstanceState() and restoring in onActivityCreated(), but it seemed like onSaveInstanceState() was never getting called.

  • In the activity, I tried saving the text in onSaveInstanceState() and restoring in onCreate(), but the EditText was null at that point.

  • I tried setting saveEnabled to true in the xml layout for the EditText which also didn't work.

What am I doing wrong?

Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
  • Have you set the ID to your EditText on xml? It's most important things that is necessary for saving texts in EditText? Also your question is duplicate this one: https://stackoverflow.com/a/19234974/2557258 – Yazon2006 Jun 26 '17 at 17:55
  • check this https://stackoverflow.com/questions/12214600/save-data-and-change-orientation – Pavan Jun 26 '17 at 17:57
  • Yes, the EditText has an ID set – Josue Espinosa Jun 26 '17 at 17:58
  • @JosueEspinosa It looks like you doing something wrong. Maybe you setting new adapter to recycler each time onCreate? It would be easier to help you if you show your full fragment code. – Yazon2006 Jun 26 '17 at 18:02
  • @Yazon2006 The view hierarchy is pretty complex, I'll add more relevant code shortly – Josue Espinosa Jun 26 '17 at 18:03
  • @Yazon2006 Yes, I think that might actually be the underlying issue. In my fragment, in `onActivityCreated()`, I create the `RecyclerView` and set the adapter. Should that not be the case? – Josue Espinosa Jun 26 '17 at 18:10
  • @JosueEspinosa if you created new instance of adapter - thats the point. Try to create it only in case if it is null to avoid unnecessary recreation of adapter. Still can't say for sure... – Yazon2006 Jun 26 '17 at 18:15
  • Hmm, wrapping the setup in `if (mAdapter == null)` still doesn't seem to solve the problem. – Josue Espinosa Jun 26 '17 at 18:21
  • share code for this. – Nishant Bhakta Jun 26 '17 at 18:44
  • you have to use protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); } It will give you back the bundle that you saved in the protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } – Sarthak Gandhi Jun 26 '17 at 20:09

2 Answers2

2

Try declaring saveEnabled = "true" in your editText inside xml layout

Tuby
  • 3,158
  • 2
  • 17
  • 36
0

put this in your Fragment onCreate()

setRetainInstance(boolean)

Read more about it here with full example code

Tord Larsen
  • 2,670
  • 8
  • 33
  • 76