what is the inter-relation between onPause and OnDestroy? why is it that when onPause is declared, onDestroy may not be called? and how do you ensure. The reason that this answer is asked because: I am trying to log the user out when the user decides to terminate app, apparently, it is inadvisable to use onDestroy, as it is only called when the device is low on memory and needs some instances.And i am unsure on how this implementation would work in onPause implementation
-
1You can study this in detail here [Android- Activity](http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle). – AndyN Sep 26 '14 at 05:03
-
1what is onDeclared :) ? – T_V Sep 26 '14 at 05:11
2 Answers
You should not use onPause to logout, If you login from the Activity A, and if you try to logout in the onPause method of Activity A, then when you go from Activity A to another Activity B, the user will be logout automatically. Because when you go to Activity B, the onPause method of Activity A is called.
Try to understand the lifecycle of the Activty, for experiment, put Log.d("youActivityName","method name"); in the methods of your Activities, and play with your app/ project, so that you can understand when are the methods being called.
I would suggest, use a Button which will close all the activities and logout from the application.
check this link: https://stackoverflow.com/a/14002030/4082061
EDIT : use the onDestroy function
@Override
protected void onDestroy() {
super.onDestroy();
//Code
}
onPause(): Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns. Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.
onDestroy(): The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

- 5,978
- 5
- 25
- 41
-
1As stated in the edited question, I am looking at ways that I could allow app to logout users when the users terminate the app. Apparently, it is inadvisable to use onDestroy as it is unpredictable, it is not called on a regular basis – Ernest Lee Sep 26 '14 at 05:17
-
I have use that onDestroy method to code the logout, however,I have discovered that the behaviour is very erratic, onDestroyed is not called on a regular basis but on certain instances where I assumed that the device is low on memory – Ernest Lee Sep 26 '14 at 05:32