Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6128343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:36:59+00:00 2026-05-23T16:36:59+00:00

I am working on an application that maintains a connection for several purposes, such

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:36:59+00:00Added an answer on May 23, 2026 at 4:36 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The application I'm working on has a single class that maintains a database connection.
I'm working on an Android application that needs to download an image and display
I am working on splitting out an existing, working application that I currently have
Im working on an application that needs to talk to a database. The application
I'm working on an application that allows users to edit invoices generated in other
We're working on an application that displays information through a Direct3D visualisation. A late
I am working on an application that installs a system wide keyboard hook. I
I am working on an application that is about 250,000 lines of code. I'm
I am working on an application that detects the most prominent rectangle in an
I'm working on an application that is implemented as an HTA. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.