保存动态生成的观点比如,当我们来回切换活动之间当我们、来回、观点、动态

2023-09-05 10:19:46 作者:灯红酒绿过后

我膨胀的按钮,单击视图,并为他喜欢的用户可以添加尽可能多的观点,我做了它的工作一切都很好,但现在的问题是,当我回去一个活动,并再次降临到我的动态生成的活动中生成的每一个观点是走了。类似的情况下,如果我去下一个活动回来的膨胀活动。我知道 onSaveInstance onRestoreSaveInstance 。但我怎么把视图信息捆绑的的onSaveInstanceState ?请注意,我的看法是动态生成的,即按钮点击,我想知道是如何preserve我的活动状态。 你怎么做呢?

I am inflating a view on button click and the user can add as many views as he likes, all is fine I made it work, but now the problem is when I go back one activity and come again to my dynamically generated activity every single view that was generated is gone. Similar is the case if I go to next activity and come back to the inflated activity. I know about onSaveInstance and onRestoreSaveInstance. But how do I put view information in a bundle in onSaveInstanceState? Please note that my view was generated Dynamically i.e. on button Click and I want to know as of how to preserve the state of my activity. How do you go about it?

推荐答案

我想,你应该实现某种逻辑,可以帮助你恢复你的状态视图 。所以,你应该设计一个类,让说 ViewDetail ,不知怎的,保持对详细信息视图要添加.. ..类型,尺寸等此类应该实现 Parcelable 所以你可以将其添加到

I am thinking that you should implement some kind of logic that helps you restore the state of your Views. So you should be designing a class, let say ViewDetail that somehow keeps details about the Views that you are adding.... type, dimension, etc. This class should implement Parcelable so you are able to add it to the bundle.

所以,你会持的ArrayList< ViewDetail> myViews ,其中每次用户添加了一个新的查看创建你添加到您的 myViews 阵列,一个新的 ViewDetail 对象

So you will keep an ArrayList<ViewDetail>, myViews where everytime the user adds a new View you create a new ViewDetail object that you add to your myViews array.

,然后保存浏览和使用这些对象的还原出来:

And then save your Views and restore them using those objects:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //save your view states
    outState.putParcelableArrayList("MY_VIEWS",myViews);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    //get the views back...
    myViews=savedInstanceState.getParcelableArrayList("MY_VIEWS");
    //TODO: add the views back to your Activity
}
 
精彩推荐
图片推荐