How to put a View into Bundle

后端 未结 4 2488
梦毁少年i
梦毁少年i 2021-02-20 11:59

I\'m adding dynamically created EditTexts to a layout (onClick button method). When I rotate the screen the added edittexts disappear. How can I add them to a bundle to put them

相关标签:
4条回答
  • 2021-02-20 12:09

    You cannot add views to a bundle because they are not parcelable / serializable. The only stuff you can and should save into the bundle is the current state of the activity.

    You could maintain a list data structure or a counter variable that keeps track of the dynamically created views. Also save the string values of the EditTexts. In onCreate you should interpret that information in order to recreate the views and their states.

    0 讨论(0)
  • 2021-02-20 12:22

    you can't save a view. bundle is just a pair name-value. of course you can save the text and then render it again. the way is just you said.

    onsaveInsatnceState(bundle save) save the text as save.putString('text1','bla bla') of course you need a loop to save all your edittext.

    then in oncreated( bundle save)

    you can get what you have added by save.getString('text1')

    0 讨论(0)
  • 2021-02-20 12:22

    Views itselfs doesn't represents information that you need to save. Instead you should save their coordinates, width, height, value etc.

    0 讨论(0)
  • 2021-02-20 12:30

    Your can save your view data into a class or into a arraylist. when your fragment activity will restart than you fetch data from saveinstance state in oncreate function.

    Use this function setRetainInstance(true). after using thing function your fragment activity will not destroy.

    0 讨论(0)
提交回复
热议问题