Android:将对象从活动传递到视图
我的活动从 res.raw 加载数据
为了将这些数据提供给视图,我向视图构造函数添加了一个参数,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.loadLibrary("engine-2d");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
if (_engine == null)
{
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
byte[] scene;
int scene_size = 0;
try {
InputStream ins = getResources().openRawResource(R.raw.package_test);
scene_size = ins.available();
scene = new byte[scene_size];
ins.read(scene);
ins.close();
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
_engine = new PlsEngine2D(scene, scene_size, width, height);
}
setContentView(new PlsSurface2D(this, _engine));
}
@Override
public void onDestroy() {
super.onDestroy();
_engine.DestroyEngine();
}
我不知道为什么,但是当我按下主页按钮时(我的程序的实例仍在内存中) 然后我再次启动该应用程序,它崩溃了。
事实上,PlsEngine2D使用ndk来调用C函数来创建malloc。
我不知道问题出在哪里,但我想知道 malloc 指针是否仍然正确!
或者这可能是我的观点! 我将 _engine 对象赋予它。 我想知道当视图唤醒时它是否再次具有 _engine param 对象!
还有另一种方法可以将我的 _engine 对象提供给视图吗? 视图是否可以将 _engine 值传递给活动?
My activity load data from res.raw
To give those datas to the view, I've added a param to the view construtor
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.loadLibrary("engine-2d");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
if (_engine == null)
{
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
byte[] scene;
int scene_size = 0;
try {
InputStream ins = getResources().openRawResource(R.raw.package_test);
scene_size = ins.available();
scene = new byte[scene_size];
ins.read(scene);
ins.close();
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
_engine = new PlsEngine2D(scene, scene_size, width, height);
}
setContentView(new PlsSurface2D(this, _engine));
}
@Override
public void onDestroy() {
super.onDestroy();
_engine.DestroyEngine();
}
I don't know why but when I press the home button (the instance of my program is still in memory)
and then I launch the application again, it crash.
In fact, PlsEngine2D use ndk to call C function that make malloc.
I do not exactly know where is the problem but I wonder if the malloc pointer are still correct !
Or may be this is my view!
I give the _engine object to it.
I wonder if when the view wake up it have the _engine param object again !
Is there another way to give my _engine object to the view ?
Does the view can take the the _engine value to the activity ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在您的问题中发布一些代码吗?我不确定我是否了解全貌。
无论如何,当您启动仍在内存中的 Activity 时,将按以下顺序调用该 Activity 的以下方法:onRestart()、onStart() 和 onResume()。
查看此链接了解详细信息
Could you post some code in your question please? I am not sure I am getting the whole picture.
Anyway, when you start an activity that is still in memory, the activity's following methods are called in the following order: onRestart(), onStart() and onResume().
Check this link for details
onResume()
当 Activity 仍在内存中时调用。它不会再次创建,因为在onResume()
中您不会再次调用setContentView()
。onResume()
is called when the activity is still in the memory. It is not created again, since inonResume()
you dont callsetContentView()
again.