ONSTART()和onResume之间差() [英] Difference between onStart() and onResume()

查看:160
本文介绍了ONSTART()和onResume之间差()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能让 ONSTART()过渡态的意义。该 onResume()方法总是被调用后 ONSTART()。为什么不能是 onResume() onRestart被调用()的onCreate ()方法只是不含 ONSTART()?其目的是什么?

为什么我们不能生活在没有 ONSTART()。我仍然认为这是多余的(可能是因为不明白它的意思是完全)。

解决方案
  

为什么不能将它的onR​​esume()被onRestart()和的onCreate()方法只是不含ONSTART()调用之后?其目的是什么?

OK,因为我的第一个答案是pretty的长,我将不会延长进一步让我们试试这个...

 公共DriveToWorkActivity延伸活动
    实现onReachedGroceryStoreListener {
}

公共GroceryStoreActivity延伸活动{}
 

请注意:我特意留了电话给像 super.onCreate(...)等,这是伪 - code所以给我一些艺术照在这里。 ;)

的方法 DriveToWorkActivity 遵循...

 保护无效的onCreate(...){
    openGarageDoor();
    unlockCarAndGetIn();
    closeCarDoorAndPutOnSeatBelt();
    putKeyInIgnition();
}

保护无效的OnStart(){
    startEngine();
    changeRadioStation();
    switchOnLightsIfNeeded();
    switchOnWipersIfNeeded();
}

保护无效onResume(){
    applyFootbrake();
    releaseHandbrake();
    putCarInGear();
    驱动();
}

保护无效的onPause(){
    putCarInNeutral();
    applyHandbrake();
}

保护无效的onStop(){
    switchEveryThingOff();
    turnOffEngine();
    removeSeatBeltAndGetOutOfCar();
    lockCar();
}

保护无效的onDestroy(){
    enterOfficeBuilding();
}

保护无效onReachedGroceryStore(...){
    意图I =新的意图(ACTION_GET_GROCERIES,......,这个,GroceryStoreActivity.class);
}

保护无效onRestart(){
    unlockCarAndGetIn();
    closeDoorAndPutOnSeatBelt();
    putKeyInIgnition();
}
 

好了,所以它的另一个长一(对不起乡亲)。但这里是我的解释......

onResume()是当我开始的时候我来暂时停止驾驶,并的onPause()是。所以,我开车,然后达到一个红灯,所以我停下......光变绿色和我重新开始。另一个红灯,我停下,然后绿色让我重新开始。该的onPause() - GT; onResume() - >的onPause() - > onResume()循环是一紧一,并通过我的旅程发生很多次了。

被停止回通过重​​新启动(preparing进行我的旅程),以重新开始循环可能是不太常见的。在一个案例中,我发现杂货店和 GroceryStoreActivity 启动(强迫我 DriveToWorkActivity 到<$点C $ C>的onStop())。当我从店里回来,我通过 onRestart() ONSTART()然后再重新开始我的旅程。

我可以把code,它是在 ONSTART()到这两个的onCreate() onRestart()并没有刻意去覆盖 ONSTART(),但在所有需要进行的<$ C $做得更C>的onCreate() - GT; onResume()和 onRestart() - GT; onResume(),更多的我复制的东西。

所以,重新报价再次...

  

为什么不能是onResume()onRestart()和之后的onCreate调用()方法只是不含ONSTART()?

如果你不覆盖 ONSTART()那么这实际上是发生了什么。虽然活动 ONSTART()方法将被隐式调用,在code的作用是有效地的onCreate() - GT; onResume() onRestart() - GT; onResume()

I can't get the meaning of onStart() transition state. The onResume() method is always called after onStart(). Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()? What is its purpose?

Why can't we live without onStart(). I still consider it as redundant (probably because don't understand its meaning completely).

解决方案

Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()? What is its purpose?

OK, as my first answer was pretty long I won't extend it further so let's try this...

public DriveToWorkActivity extends Activity
    implements onReachedGroceryStoreListener {
}

public GroceryStoreActivity extends Activity {}

PLEASE NOTE: I've deliberately left out the calls to things like super.onCreate(...) etc. This is pseudo-code so give me some artistic licence here. ;)

The methods for DriveToWorkActivity follow...

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();
}

OK, so it's another long one (sorry folks). But here's my explanation...

onResume() is when I start driving and onPause() is when I come to a temporary stop. So I drive then reach a red light so I pause...the light goes green and I resume. Another red light and I pause, then green so I resume. The onPause() -> onResume() -> onPause() -> onResume() loop is a tight one and occurs many times through my journey.

The loop from being stopped back through a restart (preparing to carry on my journey) to starting again is perhaps less common. In one case, I spot the Grocery Store and the GroceryStoreActivity is started (forcing my DriveToWorkActivity to the point of onStop()). When I return from the store, I go through onRestart() and onStart() then resume my journey.

I could put the code that's in onStart() into both onCreate() and onRestart() and not bother to override onStart() at all but the more that needs to be done between onCreate() -> onResume() and onRestart() -> onResume(), the more I'm duplicating things.

So, to requote once more...

Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()?

If you don't override onStart() then this is effectively what happens. Although the onStart() method of Activity will be called implicitly, the effect in your code is effectively onCreate() -> onResume() or onRestart() -> onResume().

这篇关于ONSTART()和onResume之间差()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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