简体   繁体   English

android保存以编程方式创建的视图

[英]android save programmatically created views

I programmatically create a RelativeLayout with some other views inside it and add it to the parent which is defined in XML. 我以编程方式创建了一个RelativeLayout,其中包含一些其他视图,并将其添加到XML定义的父级中。 But all the views (incl. the layout) that were created programmatically disappear after the activity is re-created. 但是在重新创建活动之后,以编程方式创建的所有视图(包括布局)都会消失。 Do I need a SharedPreferences object to save the values and then re-create the layout or is there an easier way to save it? 我是否需要一个SharedPreferences对象来保存值,然后重新创建布局,还是有一种更简单的方法来保存它?

PS all new created views get an id assigned PS所有新创建的视图都分配了一个ID

I recommend you look into Activity.onSaveInstanceState(Bundle) : 我建议您查看Activity.onSaveInstanceState(Bundle)

The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id , and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState()). 默认实现通过在具有id的层次结构中的 每个视图上调用onSaveInstanceState()并保存当前关注的视图的id(所有内容都由UI还原)来为您处理大部分UI每个实例状态。 onRestoreInstanceState()的默认实现。 If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation [ via super.onSaveInstanceState() and super.onRestoreInstanceState() ], otherwise be prepared to save all of the state of each view yourself. 如果您覆盖此方法以保存未由每个单独视图捕获的其他信息,则您可能希望通过[通过super.onSaveInstanceState()super.onRestoreInstanceState() ]调用默认实现,否则准备保存所有每个视图自己的状态。

https://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle) https://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

as well as my answer here: 以及我在这里的答案:

How can I assign an ID to a view programmatically? 如何以编程方式为视图分配ID?

Do I need a SharedPreferences object to save the values and then re-create the layout 我是否需要一个SharedPreferences对象来保存值,然后重新创建布局

You say that you are creating these widgets in onResume() . 您说要在onResume()中创建这些小部件。 Your code in onResume() needs to know what widgets need to be created. 您在onResume()代码需要知道需要创建哪些小部件。 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. 如果这纯粹是暂时性信息,并且您担心由于屏幕旋转或其他配置更改而导致活动被破坏并重新创建之类的事情,请在活动或片段上使用onSaveInstanceState()从旧的传递有关这些小部件的数据到活动的新实例。 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. 另一方面,如果您担心要在几个月后重新创建这些视图,则需要永久存储以下信息: SharedPreferences ,数据库,其他文件结构,“云”等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM