简体   繁体   English

Android,查看生命周期和恢复

[英]Android, View lifecycle and restore

I has a view that is create by 我有一个创建的视图

this.vehicleListUI = new GameUIVehicleList();
this.vehicleListUI.attachGameEngine(myGameEngine);

This work fin unto the android destroy and restore the view. 这个工作鳍到android销毁并恢复视图。 The Android can recreate the view by use the empty constructor, but my problem is for this GameUIVehicleList will work I most also have the myGameEngine object set to the GameUIVehicleList Android可以通过使用空构造函数重新创建视图,但我的问题是这个GameUIVehicleList将工作我大多数也将myGameEngine对象设置为GameUIVehicleList

The myGameEngine is not Serializable and can't be this to ( Is a part of UI ) myGameEngine不是Serializable,不能是这个(是UI的一部分)

Exist this a way to restore my View fully by reattach the myGameEngine object to a create view. 通过将myGameEngine对象重新附加到创建视图,可以完全恢复我的View。 This will also be good for enable a lot of "new GameUIVehicleList()" into a XML layout file. 这对于将大量“新GameUIVehicleList()”转换为XML布局文件也很有用。

EDIT: 编辑:

I don't know if this is to some help but way I has this problem from begin is I use a android.support.v4.app.FragmentActivity and this keep to destroy and recreate my View object. 我不知道这是否有一些帮助,但是我从头开始遇到这个问题的方法是我使用android.support.v4.app.FragmentActivity并继续销毁并重新创建我的View对象。 I can also fix this by override some method in android.support.v4.app.FragmentActivity that is invoke while FragmentActivity is recreate/crate a fragment and for this set the need value from my Activity that hold the need data. 我也可以通过覆盖android.support.v4.app.FragmentActivity中的一些方法来解决这个问题,这是在FragmentActivity重新创建/创建一个片段时调用的,并为此设置了我的Activity中需要保存需求数据的值。

Destroying and re-creation of Activity (and views) usually happens on configuration change (most often screen orientation change). 销毁和重新创建活动(和视图)通常发生在配置更改(最常见的是屏幕方向更改)上。 You may want to check if this article helps and onRetainNonconfigurationInstance() / setRetainInstance() would not be the cure for your problems. 您可能想要检查本文是否有帮助,而onRetainNonconfigurationInstance() / setRetainInstance()不能解决您的问题。

If myGameEngine is not Serializable or Parcelable , I do not believe it is possible to do this reliably. 如果myGameEngine不是SerializableParcelable ,我不相信这是能够可靠地做到这一点。

However if it is ok, for myGameEngine to sometimes not be recovered you can extend the Application class with your own custom MyApplication class which contains the variable myGameEngine then in your onCreate you can get the myGameEngine that saved in your application context. 但是如果myGameEngine ,有时候myGameEngine无法恢复,你可以使用自己的自定义MyApplication类扩展Application类,该类包含变量myGameEngine然后在你的onCreate你可以获得保存在应用程序上下文中的myGameEngine

MyApplication app = (MyApplication) this.getApplicationContext();
this.vehicleListUI.attachGameEngine(app.myGameEngine);

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

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