在配置更改和终止应用程序期间了解 onPause() 和 onSaveInstance()

Understanding onPause() and onSaveInstance() during configuration change and killing the application

onPause() and onSaveInstance() is always called in case of configuration changes.However in one exceptional case if programmer is handling configuration changes than in that case it will not call these methods.

"if programmer is handling configuration changes" 是什么意思?这是否意味着如果我们以编程方式改变设备的方向,那么这些方法将不会被调用?

onPause() is the only function which will be called without fail before killing the application.So we should save all persistent data in onPause() only.

但是如果我们通过调用 finish() 方法在 onCreate 中完成我们的 activity 则直接调用 onDestroy()onPause(),onStop() 不会被调用.

What does it mean by "if programmer is handling configuration changes".Does it mean that if programmatically we are changing orientation of device then these methods will not get called?

这可能是指已将 configChanges 元素添加到其清单中的开发人员。

What does it mean by "if programmer is handling configuration changes".Does it mean that if programmatically we are changing orientation of device then these methods will not get called?

这意味着您作为程序员必须手动设置配置更改时发生的情况。将调用此方法。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

因此,如果方向和其他配置发生变化,您可以处理发生的情况。

But if we are finishing our activity in onCreate by calling finish() method then onDestroy() is directly called and onPause,onStop are not called.

如果您想通过调用 finish 显式销毁 activity,您应该覆盖 onDestroy 方法并进行清理。我们讨论的是如果您的应用程序在运行时发生变化会发生什么,即 phone 发生调用、轮换变化等