Activity生命周期混乱
活动的生命周期在很多地方都有记录,但我找不到我需要的东西。这是我的活动,它有一个构造函数和 onCreate 方法。在我的项目中,我还记录了这种方法,每次从纵向转到横向时,我都会看到这两种方法都被执行。为什么我的构造函数被调用?不是堆栈中的活动,并且我的活动的实例位于内存中,因此当发生配置更改时,只有 oncreate 和 on keepistancestate 应该发生(当然是 onResume)。为什么每次都会调用构造函数,谁在调用?是否每次当配置发生更改时,都会保证调用这两个方法(按相同的顺序依次调用)。
public TestActivity()
{
super(R.menu.main_menu, tag);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
我正在玩我的示例应用程序,但我想了解更多细节,有人可以向我澄清包含构造函数时的情况吗?我建立了很多有关生命周期的文档,但没有解释包含构造函数时的详细信息
Edit1: 我在某些地方读到,活动中存在堆栈,因此下次它们启动并运行得更快,但是当配置发生更改时怎么办?是否必须调用构造函数和 oncreate 方法?
The life cycle of an activity is documented in many places but I couldn't find the thing I need. This is my activity, it have a constructor and the onCreate method. In my project I have also a logging in this methods and every time when I go from portrait to landscape I see that both methods are executed. Why my the constructor is called ? isn't the activity in the stack and the instance of my activity is in the memory so when the configuration change is happen, then only the oncreate and on retainistancestate should happen (of course the onResume). Why the constructor is called every time, who is calling ? Is it every time when something get changed from the configuration both methods are guaranteed to be called (one after another, in this same sequence).
public TestActivity()
{
super(R.menu.main_menu, tag);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
I was playing with my sample app but I want to know more details, can someone clarify me the scenario when the constructor is included ?, I founded a a lot of documentation about life-cycle but none explains the details when the constructor is included
Edit1:
I read in some places that there is stack in witch the activities are putted in so the next time they go up and running faster, but what when the configuration get changed ? Is it must to to call the constructor and the oncreate methods ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
轮换时,您的活动将重新开始完成。您可以在清单中使用
android:configChanges="keyboardHidden|orientation"
来防止这种情况发生。On rotation your activity will be restarted complete. You can prevent that with
android:configChanges="keyboardHidden| orientation"
in your manifest.正如 @rekire 回答的那样,该活动在屏幕旋转时重新启动。这里的重新启动意味着框架创建了该活动的另一个实例,这就是为什么调用活动类的构造函数然后调用 onCreate() 的原因。新的 Activity 实例会替换旧的 Activity 实例,如果旧的 Activity 实例的引用没有被其他实例持有,那么旧的 Activity 实例最终将被 GC 回收。
如果您想避免屏幕旋转时 Activity 重新启动,请阅读此问题。
我绘制了一个 UML 图来描述 Android 活动生命周期 。
As @rekire answered, the activity is restarted on screen rotation. Here restart means that the framework creates another instance of the activity, that's why the constructor of your activity class is called and then the onCreate(). The new activity instance replaces the old one which will be finally recycled by GC, if its reference isn't held by others.
If you want to avoid activity restart on screen rotation, please read this question.
I've drawn an UML diagram to describe the Android activity life cycle.
因此,没有理由让构造函数调用活动,除非您有带参数的构造函数(onCreate 无论如何都会为我们调用它......)。然而基本上它看起来像一个java的东西 onCreate 可能会调用 activties 的默认构造函数,它是
尝试与带有参数的构造函数相同的东西
希望这会有所帮助,
不过我会等待更多专家的答案:)
Edit : So when you rotate your phone which calls onCreate as it will get created again and onCreate probably calls default constructor to invoke instance of your activity :)... I forgot to mention this earlier.
Hence there are no reason having constructor to invoke activity unless you have constructor with params(onCreate invoke it for us anyway...). However basically it seems like a java thing onCreate probably calling activties's default constructor which is
Try same thing with constructor with param like
Hope this will help,
I would wait for more expert answer though :)
Edit : So when you rotate your phone which calls onCreate as it will get created again and onCreate probably calls default constructor to invoke instance of your activity :)... I forgot to mention this earlier.