Android:当用户按主页但不在屏幕旋转时如何断开连接?

发布于 2024-11-19 17:49:54 字数 566 浏览 8 评论 0原文

我正在开发一个应用程序,该应用程序出于多种目的维护连接,例如发布接收位置更新。根据android的做事方式和其他答案的建议,我没有覆盖在屏幕旋转上销毁和重新创建应用程序的默认android行为,并且在这方面事情确实工作得很好。

我保持与 onRetainNonConfigurationInstance 方法的连接。问题是,我想在用户按 Home、应用程序最小化或由于其他原因失去焦点时关闭连接,但在屏幕旋转时不会关闭连接 - 因此,我不能在 onPause、onStop 或 OnDestroy 中执行此操作,而不需要这样做一些检查,因为它们在配置更改时被依次调用。现在我使用 isFinishing() 来检查应用程序是否正在关闭 - 但用户按 Home 的情况并不需要 isFinishing() == true (这是有道理的)。

我想到的一个解决方案是检查应用程序是否将焦点集中在处理连接更新的线程中,如果一段时间没有焦点,则简单地关闭它 - 但我觉得必须有一些更好的方法来做到这一点?

预先感谢您的宝贵时间。

(在阅读发布的答案后,进行编辑以澄清有关活动生命周期和 onRetainNonConfigurationInstance 的问题)

I am working on an application that maintains a connection for several purposes, such as publishing an receiving location updates. As per the android way of doing things and advice from other answers I have not overridden the default android behavior of destroying and recreating the application on screen rotation, and things do work nicely in that regard.

I keep hold of the connection with the onRetainNonConfigurationInstance method. The problem is that I would like to close the connection when the user presses Home, the application is minimized or for some other reason loses focus but NOT when the screen is rotated - I can therefore not do this in onPause, onStop, or OnDestroy without some checks, since they are called one after the other on configuration changes. As it is right now I use isFinishing() to check whether the application is being closed - but the case where the user presses Home does not entail isFinishing() == true (which makes sense).

One solution I have thought of is checking whether the application has focus in the thread handling the connection updates and simply close it if some time has passed without focus - but I feel like there must be some better way of doing this?

Thanks in advance for your time.

(Edited to clear things up with regards to the activity lifecycle and onRetainNonConfigurationInstance, after reading the answers posted)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我三岁 2024-11-26 17:49:54

我终于在 Activity 中找到了 onUserLeaveHint() 的钩子方法,它至少对于我到目前为止所看到的情况达到了我想要的效果。也就是说,连接在由于配置更改而重新启动期间保持打开状态,但在用户按 home 或 back 时关闭。因此,我最终得到的解决方案类似于下面的代码。与问题无关的所有内容都已被删除,名称也已被简化。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (m_connection == null) {
        Connection connection = (Connection) getLastNonConfigurationInstance(); // Try to get a saved version of the connection
        if (connection != null) {
            m_connection = connection;
        }
        else {
            m_connection = new Connection(); // Else create a new one
        }
    }
}

@Override
protected void onStart() {
    super.onStart();
    // The activity is about to become visible, connect.
    if (!m_connection.isConnected())
        m_connection.connect();
}

@Override
protected void onResume() {
    super.onResume();
    // The activity has become visible (it is now "resumed").
    m_bUserLeaving = false;
}

@Override
protected void onStop() {
    super.onStop();
    // The activity is no longer visible (it is now "stopped").
    if (m_bUserLeaving && m_connection.isConnected()){ // Disconnect if we stopped because the user left
        m_connection.disconnect();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (isFinishing() && m_connection.isConnected()) { // Often means that the user pressed back
        m_connection.disconnect();
    }
}

@Override
public Object onRetainNonConfigurationInstance() {
    // If we get the chance, keep our connection for later use
    return m_connection;
}

@Override
protected void onUserLeaveHint() {
    m_bUserLeaving = true;
}

I finally found the hook method onUserLeaveHint() in Activity, which does what I want at least for the cases I have seen so far. That is, the connection is kept open during restarts due to configuration changes but are closed when the user presses home or back. Thus, the solution I ended up with is something like the code below. Everything irrelevant to the question has been snipped and names have been simplified.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (m_connection == null) {
        Connection connection = (Connection) getLastNonConfigurationInstance(); // Try to get a saved version of the connection
        if (connection != null) {
            m_connection = connection;
        }
        else {
            m_connection = new Connection(); // Else create a new one
        }
    }
}

@Override
protected void onStart() {
    super.onStart();
    // The activity is about to become visible, connect.
    if (!m_connection.isConnected())
        m_connection.connect();
}

@Override
protected void onResume() {
    super.onResume();
    // The activity has become visible (it is now "resumed").
    m_bUserLeaving = false;
}

@Override
protected void onStop() {
    super.onStop();
    // The activity is no longer visible (it is now "stopped").
    if (m_bUserLeaving && m_connection.isConnected()){ // Disconnect if we stopped because the user left
        m_connection.disconnect();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (isFinishing() && m_connection.isConnected()) { // Often means that the user pressed back
        m_connection.disconnect();
    }
}

@Override
public Object onRetainNonConfigurationInstance() {
    // If we get the chance, keep our connection for later use
    return m_connection;
}

@Override
protected void onUserLeaveHint() {
    m_bUserLeaving = true;
}
我家小可爱 2024-11-26 17:49:54

根据我的说法,您应该处理设备旋转并保留旋转完成后您想要维持的所有值/操作。
对于按下主页按钮,请使用 onPause() 停止网络活动,并使用 onResume() 重新启动网络活动

According to me, you should be handling device rotation and preserve all the values/actions that you would want to sustain after the rotation is complete.
And for the home button press, use onPause() to stop the network activity and onResume() to restart it

与风相奔跑 2024-11-26 17:49:54

您应该查看帖子以更好地了解 Activity 生命周期及其回调。无需实现一些复杂的机制或覆盖 HOME 按钮的单击。您应该简单地在 onPause() 或 onStop() 活动回调中发布您想要在关闭应用程序时运行的所有代码。您可以在上面的链接中找到它们之间的区别。希望这有帮助。

You should check this post to better understand the activity lifecycle and it's callbacks. There is no need to implement some complex mechanisms or to override the HOME button click. You should simply post all your code you want to run on closing the application in onPause() or onStop() activity callbacks. Difference between those you can find in the link above. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文