android save programmatically created views

前端 未结 2 1997
陌清茗
陌清茗 2021-01-22 08:49

I programmatically create a RelativeLayout with some other views inside it and add it to the parent which is defined in XML. But all the views (incl. the layout) that were creat

2条回答
  •  青春惊慌失措
    2021-01-22 09:10

    Do I need a SharedPreferences object to save the values and then re-create the layout

    You say that you are creating these widgets in onResume(). Your code in onResume() needs to know what widgets need to be created. If this is purely transient information, and you are worried about things like the activity being destroyed and re-created due to a screen rotation or other configuration change, use onSaveInstanceState() on the activity or fragment to pass data about these widgets from the old to the new instance of the activity. If, on the other hand, you are worried about being able to re-create these views several months later, you need to store this information persistently: SharedPreferences, database, some other file structure, "the cloud", etc.

提交回复
热议问题