Android的编程方式保存创建的视图 [英] android save programmatically created views

查看:111
本文介绍了Android的编程方式保存创建的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程方式创建里面坐了其他一些意见RelativeLayout的,并把它添加到它在XML中定义父。但是,重新创建的活动毕竟是编程方式创建的意见(包括布局)消失。我需要一个共享preferences对象保存的值,然后重新创建布局或有更简单的方式来保存呢?

P.S。所有新创建的视图会被分配一个ID

解决方案
  

我需要一个共享preferences对象保存的值,然后重新创建布局

您说您在 onResume创建这些小部件()。您的code。在 onResume()需要知道部件需要创造什么。如果这纯粹是短暂的信息,你会担心这样的事情该活动被破坏并重新创建由于屏幕旋转或其它配置更改,使用的onSaveInstanceState()上的活性或片段至约从旧的这些小部件数据传递给该活动的新实例。如果,另一方面,你担心能在几个月后重新创建这些视图,你需要持久存储这样的信息:共享preferences ,数据库,其他一些文件结构,云计算等。

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 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?

P.S. all new created views get an id assigned

解决方案

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.

这篇关于Android的编程方式保存创建的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆