安卓的onCreate onResume [英] Android onCreate onResume
问题描述
我有一个问题。
当我开始了第一次我的Android应用程序,在主要活动均的onCreate
和 onResume
被调用。但我只想叫的onCreate。
I have a problem.
When I start for the first time my android application, in the main activity both the onCreate
and the onResume
are called. but I want to be called only the onCreate.
我该怎么办?
推荐答案
据该SDK文档,你看到的是预期的行为是什么。看一看在文档的活动流程图 - 。活动生命周期
According to the SDK docs what you are seeing is the intended behavior. Have a look at the flowchart in the docs for Activity - Activity Lifecycle.
编程可以通过保持一个实例成员克服这种跟踪是否onResume已被调用之前 - 在第一次被调用时,设置变量并返回例如
Programmatically you can overcome this by keeping an instance member to track whether onResume has been called before - the first time it is called, set the variable and return e.g.
private boolean resumeHasRun = false;
@Override
protected void onResume() {
super.onResume();
if (!resumeHasRun) {
resumeHasRun = true;
return;
}
// Normal case behavior follows
}
这篇关于安卓的onCreate onResume的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!