Reputation: 788
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)
Upvotes: 1
Views: 354
Reputation: 788
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;
}
Upvotes: 1
Reputation: 40218
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.
Upvotes: 0
Reputation: 11251
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
Upvotes: 0