安卓的onCreate()得到多次调用(而不是我) [英] Android: onCreate() getting called multiple times (and not by me)

查看:234
本文介绍了安卓的onCreate()得到多次调用(而不是我)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也有一些是我不太了解现在。

There is something I don't quite understand right now.

我的主要活动类创建一个服务,这将创建一个新的线程等待TCP连接。一旦一个人进来,它会启动一个新的活动:

My main activity class creates a Service, which creates a new thread that waits for a TCP connection. Once one comes in, it will start a new activity:

Intent dialogIntent = new Intent(getBaseContext(), VoIPCall.class);
dialogIntent.putExtra("inetAddress", clientSocket.getInetAddress());
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);

在此之后,这个类的的onCreate()方法被运行。这将创建2个线程:一个记录和发送数据,另一种接收和播放数据。这些线程拥有了一个真正的while循环。

After that, the onCreate() method of that class gets run. It will create 2 threads: one records and send data, the other one receive and plays data. Those threads have a forever while loop.

由于某些原因,我注意到的onCreate()那最后一堂课被再次调用,这使我的程序崩溃。我不明白为什么又被称为只有2个线程在运行,没有用户交互。该文件说:称的上是第一次创建活动时。该活动已在运行,我不是要创建它。

For some reason, I notice that the onCreate() of that last class gets called again, which makes my program crash. I do not understand why it is called again as only the 2 threads are running, there is no user interaction. The documentation says: "Called when the activity is first created.". The activity is already running and I am not trying to create it.

可能有人请解释一下我的这种行为?

Could someone please explain me this behavior?

推荐答案

Android将重建后的某些设备配置更改你的活动。一个这样的例子是取向。你可以点击这里阅读... <一href="http://developer.android.com/guide/topics/resources/runtime-changes.html">http://developer.android.com/guide/topics/resources/runtime-changes.html

Android will recreate your activity after certain "device configuration changes". One such example is orientation. You can read more here... http://developer.android.com/guide/topics/resources/runtime-changes.html

也许东西在你的线程做一些东西,被认为是一种配置更改?

Perhaps something in your threads is doing something which is considered a configuration change?

如果是这样的话,你可能会发现它有用的扩展应用程序类,而不是做你的初始化那里。看到这个帖子... 在旋转的Andr​​oid 活动启动

If that's the case you might find it useful to extend the Application class instead and do your initialization there. See this post... Activity restart on rotation Android

心连心

这篇关于安卓的onCreate()得到多次调用(而不是我)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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