简体   繁体   English

Android:是否保证在finish()之后调用onPause()?

[英]Android: Is onPause() guaranteed to be called after finish()?

Couldn't find a solid answer to this anywhere. 无法在任何地方找到可靠的答案。 I have a method where finish() is being called, and onPause() is called afterward. 我有一个方法,其中调用finish(),然后调用onPause()。

Is onPause() guaranteed to be called after a call to finish() ? 是否保证在调用finish()后调用onPause()?

Android will generally call onPause() if you call finish() at some point during your Activity's lifecycle unless you call finish() in your onCreate() . 如果你在Activity的生命周期中的某个时刻调用finish() ,Android通常会调用onPause() ,除非你在onCreate()调用finish() onCreate()

public class MainActivity extends ActionBarActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    finish();
  }



  @Override
  protected void onPause() {
    super.onPause();
    Log.d(TAG, "onPause");
  }

  @Override
  protected void onStop() {
    super.onStop();
    Log.d(TAG, "onStop");
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy");
  }
}

Run, and observe that your log will only contain "onDestroy". 运行,并观察您的日志将只包含“onDestroy”。 Call finish() almost anywhere else and you'll see onPause() called. 几乎在其他任何地方调用finish() ,你会看到onPause()被调用。

The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed). 系统将此方法称为用户离开您的活动的第一个指示(尽管并不总是意味着活动正在被销毁)。 This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back). 这通常是您应该提交应该在当前用户会话之外保留的任何更改的地方(因为用户可能不会回来)。 so onPause() is guaranteed..The official documentation here 所以onPause()是有保证的。 这里的官方文档

EDIT 1 编辑1

onCreate() is to onDestroy() && onStart() is to onStop() && onResume() is to onPause() .. onStart() is called when onCreate() finishes its work. onCreate()onDestroy() && onStart()onStop() && onResume()onPause() .. onStart()是在onCreate()完成其工作时调用的。 if not its not called.. onResume() indicates the ui is about to be shown to the user -(An Activity's content is the screen the user sees). 如果不是它没有被调用.. onResume()表示ui即将被显示给用户 - (活动的内容是用户看到的屏幕)。 if you call finish() in onCreate() , onPause() will be skipped because onResume() was never called same goes to onStart() .. so in some cases you can say its not; 如果在onCreate()调用finish() ,则会跳过onPause()因为onResume()从未被调用过相同的onStart() ..所以在某些情况下你可以说它不是; but that will be false, because what's an Activity that is not a screen or serve as a container for screens-(Fragment). 但那将是错误的,因为什么是不是屏幕的活动或者作为屏幕的容器 - (片段)。

and why would you call finish(); 为什么你会叫完(); directly in your Activity's onCreate() ? 直接在你的Activity的onCreate() F rom how Activities work in general, onPause() will always guarantee is calling.. f光盘活动的一般工作原理, onPause()将始终保证呼吁..

是的,每当你的活动消失时,都会调用onPause。

It is a bit late but may help future visitors. 这有点晚,但可能会帮助未来的访客。

I faced the problem of onPause() not being called when I tried to make an splash screen for my app following this tutorial . 当我尝试按照本教程为我的应用程序制作启动画面时 ,我遇到了onPause()未被调用的问题。

After some fiddling around and forcing my mind I figured out why! 经过一番摆弄并强迫我的思绪,我想出了原因! the onCreate() method looks like this: onCreate()方法如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
}

So before the activity gets a chance to get started or resumed I was forcing it to be finished and so bypassing the other state method calls ! 因此,在活动有机会开始恢复之前,我强迫它完成,因此绕过其他状态方法调用

But as far as I know from the official Activity documentation in any other situation (at least normal ones!) the onPause() method is guaranteed to be called! 但据我所知,在任何其他情况下的官方Activity文档中(至少是正常的!),保证调用onPause()方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM