我无法理解onStart()
个过渡态的含义.onResume()
方法总是在onStart()
之后调用.为什么不能在onRestart()
和onCreate()
方法之后调用onResume()
,仅仅排除onStart()
?它的目的是什么?
为什么我们不能没有onStart()
.我仍然认为它是多余的(可能是因为没有完全理解它的意思).
我无法理解onStart()
个过渡态的含义.onResume()
方法总是在onStart()
之后调用.为什么不能在onRestart()
和onCreate()
方法之后调用onResume()
,仅仅排除onStart()
?它的目的是什么?
为什么我们不能没有onStart()
.我仍然认为它是多余的(可能是因为没有完全理解它的意思).
为什么不能在onRestart()和onCreate()方法之后调用onResume(),而不包括onStart()?它的目的是什么?
好的,因为我的第一个答案很长,我不会再继续了,所以让我们试试这个……
public DriveToWorkActivity extends Activity
implements onReachedGroceryStoreListener {
}
public GroceryStoreActivity extends Activity {}
PLEASE NOTE:我故意省略了对super.onCreate(...)
等的调用.这是伪代码,所以请在这里给我一些艺术许可证.;)
DriveToWorkActivity
人的方法如下...
protected void onCreate(...) {
openGarageDoor();
unlockCarAndGetIn();
closeCarDoorAndPutOnSeatBelt();
putKeyInIgnition();
}
protected void onStart() {
startEngine();
changeRadioStation();
switchOnLightsIfNeeded();
switchOnWipersIfNeeded();
}
protected void onResume() {
applyFootbrake();
releaseHandbrake();
putCarInGear();
drive();
}
protected void onPause() {
putCarInNeutral();
applyHandbrake();
}
protected void onStop() {
switchEveryThingOff();
turnOffEngine();
removeSeatBeltAndGetOutOfCar();
lockCar();
}
protected void onDestroy() {
enterOfficeBuilding();
}
protected void onReachedGroceryStore(...) {
Intent i = new Intent(ACTION_GET_GROCERIES, ..., this, GroceryStoreActivity.class);
}
protected void onRestart() {
unlockCarAndGetIn();
closeDoorAndPutOnSeatBelt();
putKeyInIgnition();
}
好的,这是另一个很长的问题(对不起,伙计们).但这是我的解释...
onResume()
分是我开始开车的时候,onPause()
分是我临时停车的时候.所以我开车,然后到了红灯,所以我停了下来……灯变绿了,我又恢复了.另一个红灯亮了,我停了一下,然后绿灯亮了,我又继续开了.onPause() -> onResume() -> onPause() -> onResume()
圈是一个很紧的圈,在我的旅途中发生了很多次.
从重新启动(准备继续我的旅程)到再次开始的循环可能不太常见.在一种情况下,我发现杂货店,GroceryStoreActivity
开始了(迫使我的DriveToWorkActivity
到onStop()
).当我从store 回来时,我经过onRestart()
和onStart()
,然后继续我的旅程.
我可以把onStart()
中的代码放到onCreate()
和onRestart()
中,根本不需要重写onStart()
,但是在onCreate() -> onResume()
和onRestart() -> onResume()
之间需要做的事情越多,我复制的东西就越多.
所以,再来一次.
为什么不能在onRestart()和onCreate()方法只排除onStart()之后调用onResume()呢?
如果不覆盖onStart()
,那么这实际上就是发生的事情.虽然Activity
的onStart()
方法将被隐式调用,但代码中的效果实际上是onCreate() -> onResume()
或onRestart() -> onResume()
.