简体   繁体   English

在方向改变时调用onDestroy被调用?

[英]Getting around onDestroy being called on orientation change?

I have a stock Nexus 5 running 4.4.2 (using ART if it matters) and I've found an interesting scenario. 我有一个运行4.4.2的股票Nexus 5(如果重要的话使用ART),我发现了一个有趣的场景。 I have this as my onDestroy() : 我有这个作为我的onDestroy()

@Override
protected void onDestroy() {
    super.onDestroy();
    t.setText("onDestroy");
    t.show();
}

It's a GPS oriented app so I'm up and walking around. 这是一个面向GPS的应用程序,所以我起来走动。 I am using the technique mentioned in this question to show a lot of debug toast messages. 我正在使用此问题中提到的技术来显示许多调试Toast消息。

Anyway, when I rotate my app, the toast appears. 无论如何,当我旋转我的应用程序时,会出现吐司。 I understand that the activity is destroyed and recreated for the new orientation, but how can I know what's really going on? 我知道活动被破坏并为新方向重新创建,但我怎么知道实际发生了什么? How can I tell when my app is REALLY getting destroyed and not just being rotated? 如何判断我的应用程序何时被破坏而不仅仅是被轮换? Similar to this question , I want to log out when a particular activity is destroyed. 此问题类似,我想在特定活动被销毁时注销。

Since Honeycomb, the isChangingConfigurations() method can be queried to check whether the Activity is being recreated due to configuration changes. 从Honeycomb开始,可以查询isChangingConfigurations()方法以检查是否由于配置更改而重新创建了Activity Alternatively, the isFinishing() method can be queried on any API level to check whether the Activity is actually being finished, or is only being destroyed temporarily by the system. 或者,可以在任何API级别查询isFinishing()方法,以检查Activity是否实际完成,或者仅由系统临时销毁。

As far as I can determine, the two methods should always return mutually consistent results in practice. 据我所知,这两种方法应该总是在实践中返回相互一致的结果。 The only point where they might have diverged is when the system kills the process to clear memory, but there are no callbacks or interaction with the app at that point. 它们可能分歧的唯一一点是系统杀死进程以清除内存,但此时没有回调或与应用程序的交互。

The documentation of the onDestroy() method mentions the use of the isFinishing() method: onDestroy()方法的文档提到了isFinishing()方法的用法:

Perform any final cleanup before an activity is destroyed. 在销毁活动之前执行任何最终清理。 This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. 这可能是因为活动正在完成(有人称为finish() ,或者因为系统暂时销毁此活动实例以节省空间。您可以使用isFinishing()方法区分这两种情况。

You can put it in a fragment with setRetainInstanceState(true) set. 您可以将其放入设置了setRetainInstanceState(true)的片段中。 Place your code in the onDestroy() method of the fragment. 将代码放在片段的onDestroy()方法中。 Then, the fragment will not be destroyed on orientation changes. 然后,在方向变化时不会破坏片段。

First of all, you should not use onDestroy() to do anything because its not guaranteed to be called. 首先,你不应该使用onDestroy()来做任何事情,因为它不能保证被调用。 I would put things on the onPause() method; 我会把东西放在onPause()方法上; I wouldn't even put things in onStop(). 我甚至不会把东西放在onStop()上。

Also, Im not sure why you want to log out a user when they navigate away from the app. 另外,我不确定为什么要在用户离开应用程序时注销用户。 I would rather implement some kind of timer on the app or server to log out after x time. 我宁愿在应用程序或服务器上实现某种计时器,以便在x时间之后注销。

Now, the answer lies in the documentation: http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges 现在,答案在于文档: http//developer.android.com/reference/android/app/Activity.html#ConfigurationChanges

You might want to override onConfigurationChanged so that your activity is not restarted. 您可能希望覆盖onConfigurationChanged,以便不重新启动您的活动。

I found a couple of solutions which are really just patterns to detect when the screen rotates. 我找到了几个解决方案,它们实际上只是在屏幕旋转时检测的模式。 Alternatively, you can determine that the device was actually destroyed by checking some static data member to see if it was initialized or not. 或者,您可以通过检查某个静态数据成员来确定是否实际销毁了该设备,以查看它是否已初始化。

Configuration changed solutions: 配置更改解决方案

The first one involves handling all of the configuration changes in the onConfigurationChanged callback. 第一个涉及处理onConfigurationChanged回调中的所有配置更改

"Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest." “请注意,只有在清单中选择了要使用configChanges属性处理的配置时,才会调用此选项。”

The second involves listening for Display.getRotation() which returns a Surface.ROTATION_* object. 第二个涉及侦听Display.getRotation() ,它返回一个Surface.ROTATION_ *对象。 Which is the new orientation of your screen relative to the natural state of the device orientation. 这是屏幕相对于设备方向的自然状态的新方向。

Again, you can use the configuration changes along with the static member. 同样,您可以将配置更改与静态成员一起使用。

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

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