in Education by
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) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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; }

Related questions

0 votes
    In android 2.3 camera preview show white screen how to solve this issue? JavaScript questions and answers, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 3, 2022 in Education by JackTerrance
0 votes
    In android 2.3 camera preview show white screen how to solve this issue? JavaScript questions and answers, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I want to calculate browser height without considering browser address bar and bottom navigation bar height. The ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I want to calculate browser height without considering browser address bar and bottom navigation bar height. The ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    In my app the user manipulates a image, rotates, pinch, zoom etc. This is make within onDraw() ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have a view with a text field in it, but the text field is at the bottom. When I click the keyboard, it pops up ... to push the rest of the view up when a keyboard comes up?...
asked Mar 9, 2021 by JackTerrance
0 votes
    I'm trying to overlay 2 ImageViews, one with the background and one with an animated (using ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I'm trying to overlay 2 ImageViews, one with the background and one with an animated (using ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Hello I am quite new to pygame and I am trying to make an intro for a game where the user hovers ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I am wrapping an AdManagerAdView in an AndroidView so I can use it in Jetpack Compose. The image fails ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I am working on a code pen and am having an issue where initially my main element would be exactly ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have created different fragment and populated some menu based on the fragment. I was getting duplicate ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm experiencing some problems regarding Twitter OAuth within an android activity. I read a lot of tutorials ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    This problem is only appearing on iOS. The application is built with react-native 0.57.7 and on ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I have a UITableView of customers where a row/customer is selected. The user can then push another view ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
...