2

When the main activity is loaded I can set the logged user's state as online by updating a remote database row. If the user exits the app I must set the user state as offline.

If the app is exited using back button I have the onDestroy() method and in there I can set the user state as offline, but if I exit using Home button and I kill the app from settings onDistroy() isn't called.

Is there a way to know when the main activity is no longer on stack, so I can update the user's state?

rogermushroom
  • 5,486
  • 4
  • 42
  • 68
dorin
  • 873
  • 2
  • 15
  • 34
  • This thread may help you. AFAIK dalvik will take care of closing of your app when required. Here is SO link with dicussion on this. http://stackoverflow.com/questions/5279292/application-close-event-in-android – kosa Jan 09 '12 at 14:49

2 Answers2

2

onStop will be triggered when user clicks on back or home button. So handle your events in onStop.

public void onStop () {
//do your stuff here
super.onStop() 
}

EDIT:

Also try

@Override
protected void onPause()

super.onPause();




}

Also you could have it check if the app is finishing with

 if (this.isFinishing()){
    //Insert your finishing code here

} 
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
1

The home button doesn't call onDestroy as the activity is still on the Activity stack where as with the back button it is generally removed. When the ActivityManager decides to remove the activity from the stack, usually after a period of inactivity or when resources are required, onDestory will be called at which point your field will be reset.

I am not able to say definitively because I don't know all the information but it would seem that removing the users logged in state when you press home (in onStart as suggested by coder_For_Life22) might be bad, as if you returned to the activity the user would have to log in again, perhaps unnecessarily and the client side session management will become even more complex.

Your method of session management seems fairly questionable anyway unless you have some sort of server side session management where, for example if there was inactivity on the session the database field would be reset.

UPDATE

The only way I can think it might be possible is using the ActivityManager.getRunningTasks() or ActivityManager.getRunningAppProcesses() and checking if your app is among them. If you kill your app like you are suggesting then your app will not be among them and thus you know. It seems like a hugely complex solution if at all possible as you would need a separate background service running (which you could call getSystemService(ACTIVITY_SERVICE) on to get the ActivityManager) just so after you kill your app you still have something running which can check for this and perform the appropriate actions.

Killing your app this way neglects the activity life cycle and thus there isn't a hook in your activity where you can perform shut down calls.

It seems much more sensible to check for inactivity on the server side and reset the field this way, people don't often kill their apps like this and when they do they aren't likely to quickly hand the phone to someone else who would be able to access the information maliciously.

If the data requires such security then you should rethink your security model.

rogermushroom
  • 5,486
  • 4
  • 42
  • 68
  • I want to make the user offline only if the app is closed. When the user presses home button and then force closes the app from settings - this is the scenario I want also to make the user as offline and I don't know how to get this. – dorin Jan 09 '12 at 14:56